简体   繁体   English

如何从 object 获取数组,索引作为 object 属性之一

[英]how to get array from object with index as one of the object property

I have an object我有一个 object

const items = {
  "123": {
    "key": 123,
    "name": "one name",
    },
    "456": {
    "key": 456,
    "name": "two name",
    },
    "789": {
    "key": 789,
    "name": "three name",
    },
    };

Need to filter this from below array, with array index as object.key需要从下面的数组中过滤,数组索引为 object.key

const myFilter = [123,789];

Code I am trying is as below but it returning array inedx as 0,1,2... but I need the index to be object.key.我正在尝试的代码如下,但它返回数组 inedx 为 0,1,2... 但我需要索引为 object.key。

let selectedItems = myFilter.map((key) => {
        return items[key];
      });

Current output:当前 output:

[0:{
  key: 123,
  name: "one name"
}, 1: {
  key: 789,
  name: "three name"
}]

Expected Output预计 Output

[123:{
  key: 123,
  name: "one name"
}, 789: {
  key: 789,
  name: "three name"
}] 

jsfiddle - https://jsfiddle.net/kb374exh/2/ jsfiddle - https://jsfiddle.net/kb374exh/2/

Your actual output is actually correct and the only possible result from mapping the myFilter array to the matching properties from items .您的实际 output 实际上是正确的,并且是将myFilter数组映射到items的匹配属性的唯一可能结果。

 const items = { "123": { "key": 123, "name": "one name", }, "456": { "key": 456, "name": "two name", }, "789": { "key": 789, "name": "three name", }, }; const myFilter = [123, 789]; const selectedItems = myFilter.map((key) => items[key]); console.log(selectedItems);

The logged output you see is including the array index.您看到的记录的 output 包括数组索引。 You likely are seeing the index included when logging in the browser.您可能会在登录浏览器时看到包含的索引。

在此处输入图像描述

If you want an array of objects where the original key is the new index then the best you can likely do is an array of length <highest key> and a bunch of "holes" that are simply undefined.如果您想要一个原始键是新索引的对象数组,那么您可能做的最好的事情就是一个长度为 <highest key> 的数组和一堆根本未定义的“洞”。

在此处输入图像描述

 const items = { "123": { "key": 123, "name": "one name", }, "456": { "key": 456, "name": "two name", }, "789": { "key": 789, "name": "three name", }, }; const myFilter = [123, 789]; const selectedItems = Object.entries(items).reduce((selectedItems, [key, value]) => { if (myFilter.includes(value.key)) selectedItems[key] = value; return selectedItems; }, []); console.log(selectedItems);

If you are ok with the result being an object then you can have more succinct output, you'll basically end up with back with an object with key-value pairs filtered out.如果你对结果是 object 没意见,那么你可以有更简洁的 output,你基本上会得到一个 object 过滤掉的键值对。

在此处输入图像描述

 const items = { "123": { "key": 123, "name": "one name", }, "456": { "key": 456, "name": "two name", }, "789": { "key": 789, "name": "three name", }, }; const myFilter = [123, 789]; const selectedItems = Object.fromEntries(Object.entries(items).filter(([, value]) => myFilter.includes(value.key))); console.log(selectedItems);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM