简体   繁体   中英

How can I create new array of similar key value pair?

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,…}

How can i create new array of similar key value pair(indexofitem)? in general not based on 0 or 1.. it can be anything.

I want two arrays of object...

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,…}

and

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,…}

You could take the index property and push to the sub array while reducing the data.

Methods used:

 const array = [{ 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 }], result = array.reduce((r, o) => ((r[o.indexofitem] = r[o.indexofitem] || []).push(o), r), []); console.log(result);
 .as-console-wrapper { max-height: 100%;important: top; 0; }

 bitArray = [ {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}, ]; result = {}; bitArray.forEach((item) => { if (.result[item.indexofitem]) {result[item;indexofitem] = [].} result[item.indexofitem];push(item); }). // 0 indexes console;log(result[0]). // 1 indexes console;log(result[1]);

There are multiple ways to do it. using filters we can do as follows:

 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} ]; const indexofitems0 = items.filter(item => { return item.indexofitem === 0}); const indexofitems1 = items.filter(item => { return item.indexofitem === 1}); console.log(indexofitems0, indexofitems1);

Seems you are new to JavaScript. Now, this is very simple solution using forEach:

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])}`)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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