简体   繁体   English

如何将具有两个属性的对象数组减少为两个 arrays,每个属性一个?

[英]How to reduce an array of objects with two properties into two arrays, one for each property?

I have an array of objects with two properties: canais and topicos :我有一个具有两个属性的对象数组: canaistopicos

 channel: Array(3)
    0: {canais: 'canal1', topicos: 'topico1'}
    1: {canais: 'canal2', topicos: 'topico2'}
    2: {canais: 'canal3', topicos: 'topico3'}

I want to split into two array of strings called canais and topicos:我想分成两个字符串数组,称为 canais 和 topicos:

canais : [ "canal1" , "canal2", "canal3" ];
topicos: [ "topico1" , "topico2", "topico3" ];

You can do你可以做

const canais = channel.map(item => item.canais)
const topicos = channel.map(item => item.topicos)

You could do this in multiple ways:您可以通过多种方式执行此操作:

  1. Map each element to a 2-element array [canais, topicos] and then transpose the result to get two arrays (here original is your array): Map 每个元素到一个 2 元素数组[canais, topicos]然后转置结果得到两个 arrays (这里original是你的数组):

     const transpose = arr => arr[0].map((x, i) => arr.map(x => x[i])) const [canais, topicos] = transpose(original.map(a => [a.canais, a.topicos]))

    See also: Transposing a 2D-array in JavaScript which is the origin of the transpose function above.另请参阅: 在 JavaScript 中转置二维数组,这是上面transpose function 的起源。

  2. Use a forEach loop and accumulate:使用forEach循环并累积:

     const canais = [] const topicos = [] original.forEach(el => { canais.push(el.canais) topicos.push(el.topicos) })
  3. Normal for loop and accumulate:正常for循环和累积:

     const allCanais = [] const allTopicos = [] for (const {canais, topicos} of original) { allCanais.push(canais) allTopicos.push(topicos) }
  4. Map two times (first extract all el.canais then all el.topicos ), though this iterates twice over the data which is probably unneeded. Map 两次(首先提取所有el.canais然后提取所有el.topicos ),尽管这对可能不需要的数据进行了两次迭代。

If you need it to be dynamic, you can use a nested for loop to go through each item and create/add to arrays based on the key, then use an object to store each array of values.如果您需要它是动态的,您可以使用嵌套的 for 循环到 go 通过每个项目并根据键创建/添加到 arrays,然后使用 object 的每个数组来存储值。

let channelNames = {};
for(let c of channel){
    for(let key in c){
        if(!channelNames[key])
            channelNames[key] = [];
        channelNames[key].push(i[key]);
    }
}

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

相关问题 在一个对象数组中结合两个不同对象数组的不同属性 - Combining different properties of two different arrays of objects in one array of objects 将具有相同属性的两个对象的方法合并到每个属性的数组中 - Merge methods of two objects with same properties into array for each property 如果一个属性也是 Angular 7 中的一个数组,则比较两个对象的 arrays? - Comparing two arrays of objects if one property is also an array in Angular 7? 使用array reduce减少对象数组的两个属性 - Using array reduce to reduce two properties of an array of objects 如何将两个数组合并为一个数组的两个对象 - how to merge two arrays into two objects of one array 如何将两个 arrays 组合成具有来自每个数组的数据的多个对象? - How to combine two arrays into multiple objects with data from each array? 将两个数组合并为具有属性值的对象数组 - Merge two arrays into an array of objects with property values 如何在javascript中将两个相同长度的数组转换为一个对象数组? - How to convert two arrays of same length into one array of objects in javascript? 将两个单独的数组与对象组合成一个数组 - combining two separate arrays into one array with objects 将两个 arrays 合并为一个对象数组 React - Merge two arrays into one Array of objects React
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM