Home > Back-end >  Create an array of object based on first value as key and second value as value in javascript
Create an array of object based on first value as key and second value as value in javascript

Time:06-24

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.

CodePudding user response:

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

;

CodePudding user response:

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. The reason the object is wrapped inside parentheses is to tell JavaScript that we're evaluating an expression. Without them the JavaScript engine assumes there is a codeblock following.

input.map(({ size, no_of_ups }) => { // <- without parentheses is assumed to be a codeblock
  return { [size]: no_of_ups };
});

CodePudding user response:

(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})).

  • Related