简体   繁体   English

基于第一个值作为键和第二个值作为javascript中的值创建一个对象数组

[英]Create an array of object based on first value as key and second value as value in javascript

Sample input:样本输入:

[{'size': '56 X 56 X 190', 'no_of_ups': 5}, {'size': '65 X 55 X 110', 'no_of_ups': 2}]

Corresponding ouput:相应的输出:

[{'56 X 56 X 190': 5}, {'65 X 55 X 110': 2}]

How do we create an array of Object from input array of Object having fixed number of keys and create new array of Object having certail value of key as key and certail value of value as value of that key.我们如何从具有固定键数的对象的输入数组创建对象数组,并创建新的对象数组,其中键的某些值作为键,值的某些值作为该键的值。

Basically, We want to transform input array of Object to value of 'size' as key and value of 'no_of_ups' as value.基本上,我们希望将 Object 的输入数组转换为'size'值作为键,将'no_of_ups'的值作为值。

One of the most readable ways of doing this would probably be in normal JavaScript.最易读的方法之一可能是使用普通的 JavaScript。

const output = input.map(({ size, no_of_ups }) => ({ [size]: no_of_ups }));

The expression ({ size, no_of_ups }) => ({ [size]: no_of_ups }) might be a bit confusing, but is just a normal arrow function .表达式({ size, no_of_ups }) => ({ [size]: no_of_ups })可能有点混乱,但它只是一个普通的箭头函数

({ size, no_of_ups }) defines the arguments. ({ size, no_of_ups })定义参数。 In this scenario a single object that we'll destructure into size and no_of_ups .在这种情况下,我们将no_of_upssizeno_of_ups的单个对象。

({ [size]: no_of_ups }) is the return value of the arrow function. ({ [size]: no_of_ups })是箭头函数的返回值。 We'll use a computed property name to dynamically set the value of the key and assign it the value of no_of_ups .我们将使用计算的属性名称来动态设置键的值并为其分配no_of_ups的值。

See ECMAScript 6 arrow function that returns an object for the reason why the return object has to be wrapped within parentheses.请参阅返回对象的ECMAScript 6 箭头函数,了解返回对象必须包含在括号中的原因。

(If the order of the arguments looks strange, it's because I'm assuming [ lodash/fp module] Here's the function that you want to execute on your argument: (如果参数的顺序看起来很奇怪,那是因为我假设 [ lodash/fp模块] 这是您要在参数上执行的函数:

f = _.compose(_.spread(_.zipObject), 
              _.unzip,
              _.map(_.over(['size', 'no_of_ups'])));

And here it is in action:它在行动中:

 arr = [{'size': '56 X 56 X 190', 'no_of_ups': 5}, {'size': '65 X 55 X 110', 'no_of_ups': 2}]; f = _.compose(_.spread(_.zipObject), _.unzip, _.map(_.over(['size', 'no_of_ups']))); console.log(f(arr))
 <script src="https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)"></script>

And here's a bit of explanation:这里有一点解释:

  • _.map(_.over(['size', 'no_of_ups'])) runs _.over(['size', 'no_of_ups']) on each element, _.map(_.over(['size', 'no_of_ups'])) _.over(['size', 'no_of_ups'])
    • and _.over(['size', 'no_of_ups']) simply picks 'size' and the 'no_of_ups' of the element and puts the two results in an array), so you get an array of arrays, in this case [["56 X 56 X 190", 5], ["65 X 55 X 110", 2]] ;_.over(['size', 'no_of_ups'])只是选择元素的'size''no_of_ups'并将这两个结果放入一个数组中),所以你得到一个数组数组,在这种情况下[["56 X 56 X 190", 5], ["65 X 55 X 110", 2]] ;
  • then _.unzip fundamentally transposes the array of arrays, in this case giving you [["56 X 56 X 190", "65 X 55 X 110"], [5, 2]]然后_.unzip从根本上转置数组数组,在这种情况下给你[["56 X 56 X 190", "65 X 55 X 110"], [5, 2]]
  • finally _.spread(_.zipObject) feeds the two inner arrays as comma separated arguments to _.zipObject , which constructs objects out of the two arrays, using them as the array of keys and the array of values, thus giving you the final result.最后_.spread(_.zipObject)将两个内部数组作为逗号分隔的参数提供给_.zipObject ,它从两个数组中构造对象,将它们用作键数组和值数组,从而为您提供最终的结果。

If you really want an array of objects rather than a single object, you can change _.zipObject for _.zipWith((x,y) => ({[x]: y})) .如果您真的想要一个对象数组而不是单个对象,您可以将_.zipObject更改为_.zipWith((x,y) => ({[x]: y}))

Input - [
  {
    'size': '56 X 56 X 190',
    'no_of_ups': 5
  }, {
    'size': '65 X 55 X 110',
    'no_of_ups': 2
  }
]

Input.forEach((val) => { 
  j = {};
  j[val[size]] = val[no_of_ups];
  Output.push(j)
});

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

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