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

发布于 2025-02-10 01:34:48 字数 449 浏览 2 评论 0原文

示例输入:

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

对应的OUPUT:

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

如何从具有固定数量的键的对象的输入数组中创建一个对象数组,并创建具有键值的新的对象数组,该数组将密钥的certail值作为键和certail值作为该密钥的值。

基本上,我们希望将对象的输入数组转换为'size'的值,为'no_of_ups'的值。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳男子 2025-02-17 01:34:48

这样做的最可读性方法之一可能是在正常的JavaScript中。

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

expression ({size,no_of_ups})=> ({[[size]:no_of_ups})可能有点令人困惑,但只是一个普通的 arrow函数

({size,no_of_up})定义了参数。在这种情况下,我们将破坏进入大小no_of_ups

({[size]:no_of_up})是箭头函数的返回值。我们将使用要动态设置密钥的值并为其分配no_of_ups的值。

参见 ecmascript 6箭头函数,返回对象出于此原​​因为什么必须将返回对象包裹在括号中。

One of the most readable ways of doing this would probably be in normal 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 }) defines the arguments. In this scenario a single object that we'll destructure into size and no_of_ups.

({ [size]: no_of_ups }) is the return value of the arrow function. We'll use a computed property name to dynamically set the value of the key and assign it the value of 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.

爱要勇敢去追 2025-02-17 01:34:48

(如果参数的顺序看起来很奇怪,那是因为我假设[lodash/fp模块]
这是您要在参数上执行的函数:

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

这​​是在行动中:

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>

这是一些解释:

  • _。MAP(_。over(['size','no_of_ups'])))运行_。over(['size','no_of_ups'] )在每个元素上,
    • _。_。元素并将两个结果放入数组),因此您会得到一系列数组,在这种情况下,[[“ 56 x 56 x 190”,5],[“ 65 x 55 x 110”,2] ];


  • 然后_。unzip从根本上转置数组数组,在这种情况下为您提供[[“ 56 x 56 x 190”,“ 65 x 55 x 110”],[5,2] ]
  • 最后_。vrize(_。zipobject)将两个内部数组作为逗号分隔的参数馈送到_。zipobject,它将构造在两个数组中的对象,使用它们作为键和值数组,从而为您提供最终结果。

如果您真的想要一个对象而不是一个对象,则可以更改_。ZipObject _。zipwith(((x,x,y)=&gt;({[x]:y) }))

(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:

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,
    • 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]];
  • 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]]
  • 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.

If you really want an array of objects rather than a single object, you can change _.zipObject for _.zipWith((x,y) => ({[x]: y})).

放手` 2025-02-17 01:34:48
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)
});
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)
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文