如何从 `Object.keys()` 的 `filter` 方法中返回值(对象),而不是键?

huangapple go评论64阅读模式
英文:

How can I return value(object) form filter method on Object.keys() instead of key?

问题

const notes = {
  'jk2334|notes-md-23': {
    id: 'notes-md-23',
    text: 'First Note'
  },
  'jk2334|notes-xd-34': {
    id: 'notes-xd-34',
    text: 'Second Note'
  },
  'fd4345|notes-mf-54': {
    id: 'notes-mf-54',
    text: 'Third Note'
  }
}

const result = Object.keys(notes).filter(note => {
  if (note.startsWith('jk2334')) {
    console.log('from inner -', notes[note]);
    return notes[note];
  }
})

console.log(result)

如果运行此代码,它仅返回键而不返回对象的值。但如果在条件内部使用console.log,则返回值。

我希望返回值的数组,而不是键。现在我该怎么做?

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const notes = {
  &#39;jk2334|notes-md-23&#39;: {
    id: &#39;notes-md-23&#39;,
    text: &#39;First Note&#39;
  },
  &#39;jk2334|notes-xd-34&#39;: {
    id: &#39;notes-xd-34&#39;,
    text: &#39;Second Note&#39;
   },
   &#39;fd4345|notes-mf-54&#39;: {
    id: &#39;notes-mf-54&#39;,
    text: &#39;Third Note&#39;
   }
}

const result = Object.keys(notes).filter(note =&gt; {
        if(note.startsWith(&#39;jk2334&#39;)) {
            console.log(&#39;from inner -&#39;, notes[note]);
            return notes[note];
        }
    })

console.log(result)

<!-- end snippet -->

If I run this code it returns only key but not the value of the object. But if I console inside the condition it returns the value.

I want to return the array of value not key. What should I do now?

答案1

得分: 5

Array#filter 方法的回调函数应返回一个布尔值。如果要保留该值,则返回 true,如果不需要该值,则返回 false。不能使用该方法将输入值转换为不同的输出值。你的代码之所以偶然起作用,是因为对象是真值(truthy values)。

要将输入值转换为输出值,可以在过滤后调用 Array#map 方法,将键映射回值:

const notes = {
  'jk2334|notes-md-23': {
    id: 'notes-md-23',
    text: 'First Note'
  },
  'jk2334|notes-xd-34': {
    id: 'notes-xd-34',
    text: 'Second Note'
  },
  'fd4345|notes-mf-54': {
    id: 'notes-mf-54',
    text: 'Third Note'
  }
}

const result = Object.keys(notes)
  .filter(note =>  note.startsWith('jk2334'))
  .map(key => notes[key])

console.log(result)

以上是你提供的代码的翻译。

英文:

The callback to Array#filter is expected to return a boolean value. true if you want to keep the value and false if you don't. You cannot use it to convert input values to different output values. The code you have works accidentally because objects are truthy values.

To convert input to output values you can call Array#map after filtering to map the keys back to values:

<!-- begin snippet: js hide: false console: true babel: null -->

<!-- language: lang-js -->

const notes = {
  &#39;jk2334|notes-md-23&#39;: {
    id: &#39;notes-md-23&#39;,
    text: &#39;First Note&#39;
  },
  &#39;jk2334|notes-xd-34&#39;: {
    id: &#39;notes-xd-34&#39;,
    text: &#39;Second Note&#39;
   },
   &#39;fd4345|notes-mf-54&#39;: {
    id: &#39;notes-mf-54&#39;,
    text: &#39;Third Note&#39;
   }
}

const result = Object.keys(notes)
  .filter(note =&gt;  note.startsWith(&#39;jk2334&#39;))
  .map(key =&gt; notes[key])

console.log(result)

<!-- end snippet -->

答案2

得分: -1

你可以结合筛选和映射来缩小数组。

const
    notes = { 'jk2334|notes-md-23': { id: 'notes-md-23', text: 'First Note' }, 'jk2334|notes-xd-34': { id: 'notes-xd-34', text: 'Second Note' }, 'fd4345|notes-mf-54': { id: 'notes-mf-54', text: 'Third Note' } },
    result = Object
        .entries(notes)
        .reduce((r, [k, v]) => k.startsWith('jk2334') ? [...r, v] : r, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
英文:

You could combine filtering and mapping with reducing the array.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const
    notes = { &#39;jk2334|notes-md-23&#39;: { id: &#39;notes-md-23&#39;, text: &#39;First Note&#39; }, &#39;jk2334|notes-xd-34&#39;: { id: &#39;notes-xd-34&#39;, text: &#39;Second Note&#39; }, &#39;fd4345|notes-mf-54&#39;: { id: &#39;notes-mf-54&#39;, text: &#39;Third Note&#39; } },
    result = Object
        .entries(notes)
        .reduce((r, [k, v]) =&gt; k.startsWith(&#39;jk2334&#39;) ? [...r, v] : r, []);

console.log(result);

<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; top: 0; }
<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月16日 18:43:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/75471113.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定