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

时间:2020-04-29 14:11:02

标签: javascript

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

如何创建类似键值对(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,…}

4 个答案:

答案 0 :(得分:1)

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

使用的方法:

  • Array#reduce,其中数组作为累加器r,对象o作为项目,

  • 一个logical OR ||,用于检查属性是否为truthy以及是否不使用数组。

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; }

答案 1 :(得分:1)

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]);

答案 2 :(得分:0)

有多种方法可以做到这一点。使用过滤器,我们可以执行以下操作:

m = zip(*[v.array[0].split() for k,v in df.items()])
pd.DataFrame(m).set_axis(df.columns,axis=1)

    id  version required
0   X   1   tur
1   Y   2   mir
2   Z   3   tur

答案 3 :(得分:0)

您似乎不熟悉JavaScript。现在,这是使用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])}`)
})