Javascript 如何创建类似键值对的新数组?

Javascript 如何创建类似键值对的新数组?,javascript,Javascript,如何创建类似键值对(indexofitem)的新数组?通常不基于0或1。。它可以是任何东西 我想要两个对象数组 0: {indexofitem: 0, indexofchild_1: 0, name: "dfds", setName: "SET A", quantity: 2, price: 500,…} 1: {indexofitem: 0, indexofchild_1: 1, name: "bvcxvb", setName: "SET B", quantity: 2, price: 500

如何创建类似键值对(indexofitem)的新数组?通常不基于0或1。。它可以是任何东西

我想要两个对象数组

0: {indexofitem: 0, indexofchild_1: 0, name: "dfds", setName: "SET A", quantity: 2, price: 500,…}
1: {indexofitem: 0, indexofchild_1: 1, name: "bvcxvb", setName: "SET B", quantity: 2, price: 500,…}
2: {indexofitem: 0, indexofchild_1: 2, name: "vcx", setName: "SET C", quantity: 2, price: 500,…}
3: {indexofitem: 1, indexofchild_1: 3, name: "vxc", setName: "SET A", quantity: 1, price: 500,…}
4: {indexofitem: 1, indexofchild_1: 4, name: "vcx", setName: "SET B", quantity: 1, price: 500,…}
5: {indexofitem: 1, indexofchild_1: 5, name: "cxvxz", setName: "SET C", quantity: 2, price: 500,…}


您可以使用index属性并在减少数据量的同时推送到子数组

使用的方法:

  • 数组作为累加器
    r
    ,对象
    o
    作为项

  • 用于检查属性是否为数组,以及是否为数组的

const
数组=[{indexofitem:0,indexofchild_1:0,名称:“dfds”,集合名:“SET A”,数量:2,价格:500},{indexofitem:0,indexofchild_1:1,名称:“bvcxvb”,集合名:“SET B”,数量:2,价格:500},{indexofitem:0,indexofchild_1:2,名称:“vxx”,集合名:“SET C”,数量:2,价格:500},{indexofchild∗,集合名称:“集合A”,数量:1,价格:500},{indexofitem:1,indexofchild_1:4,名称:“vcx”,集合名称:“集合B”,数量:1,价格:500},{indexofitem:1,indexofchild_1:5,名称:“cxvxz”,集合名称:“集合C”,数量:2,价格:500},
结果=数组。减少((r,o)=>
((r[o.indexofitem]=r[o.indexofitem]| | |[])。推(o,r),[]);
控制台日志(结果)

。作为控制台包装{max height:100%!important;top:0;}
有多种方法可以做到这一点。使用过滤器,我们可以执行以下操作:

const项=[
{indexofitem:0,indexofchild_1:0,名称:“dfds”,集合名称:“集合A”,数量:2,价格:500},
{indexofitem:0,indexofchild_1:1,名称:“bvcxvb”,集合名称:“集合B”,数量:2,价格:500},
{indexofitem:0,indexofchild_1:2,名称:“vcx”,集合名称:“集合C”,数量:2,价格:500},
{indexofitem:1,indexofchild_1:3,名称:“vxc”,集合名称:“集合A”,数量:1,价格:500},
{indexofitem:1,indexofchild_1:4,名称:“vcx”,集合名称:“集合B”,数量:1,价格:500},
{indexofitem:1,indexofchild_1:5,名称:“cxvxz”,集合名称:“集合C”,数量:2,价格:500}
];
const indexofitems0=items.filter(item=>{return item.indexofitem==0});
const indexofitems1=items.filter(item=>{return item.indexofitem==1});
console.log(indexofitems0,indexofitems1)
位数组=[
{indexofitem:0,indexofchild_1:0},
{indexofitem:1,indexofchild_1:1},
{indexofitem:0,indexofchild_1:2},
{indexofitem:1,indexofchild_1:3},
{indexofitem:0,indexofchild_1:4},
{indexofitem:1,indexofchild_1:5},
];
结果={};
bitArray.forEach((项)=>{
如果(!result[item.indexofitem]){result[item.indexofitem]=[];}
结果[item.indexofitem].push(item);
});
//0索引
console.log(结果[0]);
//1索引

console.log(结果[1])您似乎是JavaScript新手。现在,这是使用forEach的非常简单的解决方案:

3: {indexofitem: 1, indexofchild_1: 3, name: "vxc", setName: "SET A", quantity: 1, price: 500,…}
4: {indexofitem: 1, indexofchild_1: 4, name: "vcx", setName: "SET B", quantity: 1, price: 500,…}
5: {indexofitem: 1, indexofchild_1: 5, name: "cxvxz", setName: "SET C", quantity: 2, price: 500,…}

问题和问题不清楚。预期的结果也是如此result@brk问题更新,希望有意义…非常感谢。。。你能提供任何链接来了解更多关于这个方法的信息吗。MDN仅包含基本信息。感谢您的回答,但正如我所说的,我希望得到更概括的答案。。。不是基于0或1。@KunwarSagar。。。此方法是如何不使用
forEach
的示例。
3: {indexofitem: 1, indexofchild_1: 3, name: "vxc", setName: "SET A", quantity: 1, price: 500,…}
4: {indexofitem: 1, indexofchild_1: 4, name: "vcx", setName: "SET B", quantity: 1, price: 500,…}
5: {indexofitem: 1, indexofchild_1: 5, name: "cxvxz", setName: "SET C", quantity: 2, price: 500,…}
const items = [
    {indexofitem: 0, indexofchild_1: 0, name: "dfds", setName: "SET A", quantity: 2, price: 500},
    {indexofitem: 0, indexofchild_1: 1, name: "bvcxvb", setName: "SET B", quantity: 2, price: 500},
    {indexofitem: 0, indexofchild_1: 2, name: "vcx", setName: "SET C", quantity: 2, price: 500},
    {indexofitem: 1, indexofchild_1: 3, name: "vxc", setName: "SET A", quantity: 1, price: 500},
    {indexofitem: 1, indexofchild_1: 4, name: "vcx", setName: "SET B", quantity: 1, price: 500},
    {indexofitem: 1, indexofchild_1: 5, name: "cxvxz", setName: "SET C", quantity: 2, price: 500}
];

//object with indexofitem value as key and list of all items with same value indexofitem as value
let indexofitemObjs = {};
items.forEach(item => {
    indexofitemObjs[item.indexofitem] ? indexofitemObjs[item.indexofitem].push(item) : indexofitemObjs[item.indexofitem] = [item]
})

Object.keys(indexofitemObjs).forEach(indexItem => {
    console.log(`Array List of: ${indexItem} : \n ${JSON.stringify(indexofitemObjs[indexItem])}`)
})