简体   繁体   English

我想根据相似对象数组中的键动态创建对象值数组

[英]I want to dynamically create arrays of object values based on their keys from an array of similar objects

Here's my JavaScript array of objects: 这是我的JavaScript对象数组:

inventoryArray = [
    {name: 'pizza', category: 'food', unique: 'no', amount: 12}
    {name: 'bow', category: 'weapon', unique: 'no', amount: 1}
    {name: 'pizza', category: 'food', unique: 'yes', amount: 1}
]

They all have the same keys and most have different values. 它们都具有相同的键,并且大多数具有不同的值。

I want to create an array of values based on keys dynamically that looks like this: 我想基于键动态创建一个值数组,如下所示:

values = [
    ['pizza', 'bow', 'pizza'] // name
    ['food', 'weapon', 'food'] // category
    ['no', 'no', 'yes'] //unique
    [12, 1, 1] // amount
    // TODO only add distinct values:
    // i.e. first array would be ['pizza', 'bow']
]

Closest I've gotten is: 我最近得到的是:

const keys = [];
const values = [];
for(let i = 0; i < inventoryArray.length; i++){
    keys[i] = Object.keys(inventoryArray[i]);
}
for(let i = 0; i < keys[0].length; i++) {
    const value = [];
    for(let j = 0; j < inventoryArray.length; j++) {            
        value[j] = inventoryArray[j][keys[j][i]];
    }
    values[i] = value;
}

Yeah, I know it's ugly. 是的,我知道这很丑。

That keys[0] in the second for loop is not really dynamic. 第二个for循环中的keys[0]并不是真正动态的。 Right? 对?

I'm still pretty new at JavaScript and I tried playing around with the map method for arrays, but couldn't get it to create them all dynamically. 我在JavaScript上还很陌生,我尝试使用数组的map方法,但是无法动态创建它们。 Just one by one: 一一:

const names = inventoryArray.map(a => a.name);

Thanks in advance! 提前致谢!

Create an array of the props you want in the correct order. 以正确的顺序创建所需道具的数组。 Use nested Array.map() calls to create the matrix. 使用嵌套的Array.map()调用创建矩阵。 You can use a Set , to make the values distinct. 您可以使用Set来使值区分。

 const inventoryArray = [{"name":"pizza","category":"food","unique":"no","amount":12},{"name":"bow","category":"weapon","unique":"no","amount":1},{"name":"pizza","category":"food","unique":"yes","amount":1}] const props = ['name', 'category', 'unique', 'amount'] const result = props.map(p => [...new Set(inventoryArray.map(o => o[p]))] ) console.log(result) 

暂无
暂无

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

相关问题 根据相似的值将对象拆分为对象数组 - Split an object in to array of objects based on similar values 如何根据 object 值从两个 arrays 创建一个新的对象数组? - How to create a new array of objects from two arrays based on object values? 如何从一个对象创建一个字符串数组,其中各个键的值是单独的字符串数组? - How do I create an array of strings from an object where the values of individual keys are separate arrays of strings? 基于两个或多个键的对象数组和动态收集的值数组对对象进行分组 - Group objects from an array of objects based on two or more keys and from an array of dynamically collected values 使用数组值从对象动态创建对象数组 - Dynamically create array of objects from the object with the array values 当对象数组在每个 object 中具有不同的键时,我想根据不同的键进行过滤 - When array of objects has different keys in each object I want to filter based on the different keys 根据对象数组中的键创建一个对象 - Create an object based on the keys in array of objects 根据对象键创建对象数组 - Create array of objects based on object keys 从基于两个键的对象的两个数组中查找唯一值,即在JavaScript中从and到 - finding unique values from two arrays of objects based on two keys i.e from and to in javascript 通过javascript对象键创建对象数组 - Create array of objects from javascript object keys
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM