javascript 获取精确索引中的数组对象

tquggr8v  于 2023-02-11  发布在  Java
关注(0)|答案(3)|浏览(128)

我有一个数组

let items=[5,1,3,4,2]

和对象数组

let objectItem=[
            {id:"item-1", name: "Total revenue",selected:true, order: 1},
            {id:"item-2", name: "Total tax",selected:true, order: 2},
            {id:"item-3", name: "Sales Report",selected:false, order: 3},
            {id:"item-4", name: "Total parking",selected:false, order: 4},
            {id:"item-5", name: "Sales maximum",selected:false, order: 5},
            {id:"item-6", name: "Amount Collected",selected:true, order: 6},
            {id:"item-7", name: "Total fall",selected:false, order: 7}
        ]

我需要得到数组objectItem,其顺序是它将order键与item数组元素精确匹配的顺序。
得到的数组应该是这样的

objectItem=[
        {id:"item-5", name: "Sales maximum",selected:false, order: 5},
        {id:"item-1", name: "Total revenue",selected:true, order: 1},
        {id:"item-3", name: "Sales Report",selected:false, order: 3},
        {id:"item-4", name: "Total parking",selected:false, order: 4},
        {id:"item-2", name: "Total tax",selected:true, order: 2},
    ]

到目前为止,我能够acheieve对象项目数组在这升序的顺序键后应用

const itemArr=objectItem.filter((itm)=>{
    return items.indexOf(itm.order) > -1;
  });

是否有任何变通方法可以获得包含对象的结果数组,这些对象的顺序值与items数组的精确索引相同

clj7thdc

clj7thdc1#

let items=[5,1,3,4,2]

let objectItem=[
  {id:"item-1", name: "Total revenue",selected:true, order: 1},
  {id:"item-2", name: "Total tax",selected:true, order: 2},
  {id:"item-3", name: "Sales Report",selected:false, order: 3},
  {id:"item-4", name: "Total parking",selected:false, order: 4},
  {id:"item-5", name: "Sales maximum",selected:false, order: 5},
  {id:"item-6", name: "Amount Collected",selected:true, order: 6},
  {id:"item-7", name: "Total fall",selected:false, order: 7}
]

console.log(items.map(i=>objectItem.find(({order:o})=>o===i)))
5q4ezhmt

5q4ezhmt2#

你可以拿一个物体,然后按照想要的顺序得到物品。

const
    items = [5, 1, 3, 4, 2],
    objectItem = [{ id: "item-1", name: "Total revenue", selected: true, order: 1 }, { id: "item-2", name: "Total tax", selected: true, order: 2 }, { id: "item-3", name: "Sales Report", selected: false, order: 3 }, { id: "item-4", name: "Total parking", selected: false, order: 4 }, { id: "item-5", name: "Sales maximum", selected: false, order: 5 }, { id: "item-6", name: "Amount Collected", selected: true, order: 6 }, { id: "item-7", name: "Total fall", selected: false, order: 7 }],
    hash = Object.fromEntries(objectItem.map(o => [o.id, o])),
    result = items.map(i => hash[`item-${i}`]);

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

ax6ht2ek3#

避免map ping任务的几乎二次努力的方法可以基于两个单独的(非嵌套的)迭代循环,其中对于每个迭代步骤,map ping任务依赖于嵌套的也迭代的find任务。

  • X1 E2 F1 X任务,其创建基于X1 E3 F1 X和X1 M4 N1 X密钥的X1 M5 N1 X查找。
  • 最后的map任务通过迭代项目优先级数组/列表并通过当前处理的order值获得每个项目来创建正确排序的项目列表,其中后者是查找的关键字。
const listOfItems = [
  { id: "item-1", name: "Total revenue", selected: true, order: 1 },
  { id: "item-2", name: "Total tax", selected: true, order: 2 },
  { id: "item-3", name: "Sales Report", selected: false, order: 3 },
  { id: "item-4", name: "Total parking", selected: false, order: 4 },
  { id: "item-5", name: "Sales maximum", selected: false, order: 5 },
  { id: "item-6", name: "Amount Collected", selected: true, order: 6 },
  { id: "item-7", name: "Total fall", selected: false, order: 7 },
];
const itemPrecedenceList = [5, 1, 3, 4, 2];

// - one time iteration in order to create a
//   `Map` and `order`-key based `item`-lookup.
const itemLookup = listOfItems
  .reduce((lookup, item) => {
    lookup.set(item.order, item);
    return lookup;
  }, new Map);

// - one time iteration in order to
//   create a correctly ordered item-list.
const reorderedItems = itemPrecedenceList
  .map(order => itemLookup.get(order));

console.log({
  reorderedItems,
  itemPrecedenceList,
});
.as-console-wrapper { min-height: 100%!important; top: 0; }

相关问题