push the contents of array into another array without looping - javascript
Here's a simple piece of JavaScript where I want to add the contents of orders.foo and orders2.foo to a single-dimensional ordersArr.
let _ = require('underscore');
let ordersArr = [];
let orders = {
foo: [
{x: 1, b: 2},
{y: 1, c: 3},
{a: 2, d: 4}
]
}
ordersArr = _.map(orders.foo, order => order)
orders2 = {
foo: [
{x: 2, b: 3},
{y: 5, c: 4},
{a: 3, d: 6}
]
}
let tOrders = _.map(orders2.foo, order => order);
ordersArr.push(tOrders)
console.log(ordersArr);
The problem with this code is that push in this case creates a multi-dimensional array:
Output
[
{ x: 1, b: 2 },
{ y: 1, c: 3 },
{ a: 2, d: 4 },
[ { x: 2, b: 3 }, { y: 5, c: 4 }, { a: 3, d: 6 } ]
]
How do I iterate the contents of orders.foo and orders2.foo and have their results as one single dimension array?
You can spread the content of both arrays into the new array
const arr1 = [1,2,3];
const arr2 = [4,5,6];
const arr3 = [...arr1, ...arr2];
console.log(arr3);
// prints [1,2,3,4,5,6]
Spreading arr2 into arr1 also works.
arr1.push(...arr2);
console.log(arr1);
// prints [1,2,3,4,5,6]
So changing
ordersArr.push(tOrders)
to
ordersArr.push(...tOrders);
should work.
For a full answer:
let ordersArr = [];
let orders = {
foo: [
{x: 1, b: 2},
{y: 1, c: 3},
{a: 2, d: 4}
]
}
orders2 = {
foo: [
{x: 2, b: 3},
{y: 5, c: 4},
{a: 3, d: 6}
]
}
ordersArr.push(...orders.foo, ...orders2.foo);
Using underscore _.flatten:
const
orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] },
orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] };
const ordersArr = _.flatten([orders.foo, orders2.foo]);
console.log(ordersArr);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"></script>
Using javascript spread operator:
const
orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] },
orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] };
const ordersArr = [...orders.foo, ...orders2.foo];
console.log(ordersArr);
Using javascript Array#concat:
const
orders = { foo: [ {x: 1, b: 2}, {y: 1, c: 3}, {a: 2, d: 4} ] },
orders2 = { foo: [ {x: 2, b: 3}, {y: 5, c: 4}, {a: 3, d: 6} ] };
const ordersArr = orders.foo.concat(orders2.foo);
console.log(ordersArr);
The spread operator mentioned above is the best 2021 way to do it.
let ordersArr = [...orders.foo, ...orders2.foo];
Use Array.concat()
let orders1 = {
foo: [
{x: 1, b: 2},
{y: 1, c: 3},
{a: 2, d: 4}
]
};
let orders2 = {
foo: [
{x: 2, b: 3},
{y: 5, c: 4},
{a: 3, d: 6}
]
};
console.log( orders1.foo.concat(orders2.foo) );
You can use concat() to merge the arrays and create a single new array:
let tOrders = orders.foo.concat(orders2.foo);
let ordersArr = [];
let orders = {
foo: [
{x: 1, b: 2},
{y: 1, c: 3},
{a: 2, d: 4}
]
}
ordersArr = _.map(orders.foo, order => order)
orders2 = {
foo: [
{x: 2, b: 3},
{y: 5, c: 4},
{a: 3, d: 6}
]
}
let tOrders = orders.foo.concat(orders2.foo);
console.log(tOrders)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.2/underscore-min.js"></script>
Another option using flat()
let ordersArr = [];
let orders = {
foo: [
{x: 1, b: 2},
{y: 1, c: 3},
{a: 2, d: 4}
]
}
orders2 = {
foo: [
{x: 2, b: 3},
{y: 5, c: 4},
{a: 3, d: 6}
]
}
let tOrders = [orders.foo, orders2.foo].flat();
console.log(tOrders)
Immutable merge of arrays
Creates a new array.
Merge using the spread operator
let orders = {
foo: [
{x: 1, b: 2},
{y: 1, c: 3},
{a: 2, d: 4}
]
}
let orders2 = {
foo: [
{x: 2, b: 3},
{y: 5, c: 4},
{a: 3, d: 6}
]
}
const mergeResult = [...orders.foo, ...orders2.foo];
console.log(mergeResult);
Merge using array.concat() method
let orders = {
foo: [
{x: 1, b: 2},
{y: 1, c: 3},
{a: 2, d: 4}
]
}
let orders2 = {
foo: [
{x: 2, b: 3},
{y: 5, c: 4},
{a: 3, d: 6}
]
}
const mergeResult = orders.foo.concat(orders2.foo);
console.log(mergeResult);
Mutable merge of arrays
Merge it into existing array.
Merge using array.push() method
let orders = {
foo: [
{x: 1, b: 2},
{y: 1, c: 3},
{a: 2, d: 4}
]
}
let orders2 = {
foo: [
{x: 2, b: 3},
{y: 5, c: 4},
{a: 3, d: 6}
]
}
orders.foo.push(...orders2.foo);
console.log(orders.foo);
I'll add one more flavor to the list. You can create a shallow copy of an array using the built-in slice method, which has been with us for a very long time:
var ordersArr = orders.foo.slice();
Now you can add the contents of the other array using push and apply:
ordersArr.push.apply(ordersArr, orders2.foo);
Et voilá, ordersArr is now a one-dimensional array containing all elements of both orders.foo and orders2.foo. Works even in ES3!
For inspiration, you can find lots of nice little tricks like this in the Underscore source code.
i think this will work for you.
let tOrders = _.map(orders2.foo, order => order);
tOrders.foreach((element)=>{
ordersArr.push(element)
})
console.log(ordersArr);
Related
How to convert an array containing arrays of objects to single array witht hose object using Ramda expressions?
I am having a difficult time, there is some bad mapping going on on my code. I have an array containing array of objects like that : [ [{a: 1, b: 2},{a: 1, b: 3} ], [{a: 5, b: 2},{a: 2, b: 5}] ] And I want to make like that : [ {a: 1, b: 2}, {a: 1, b: 3}, {a: 5, b: 2}, {a: 2, b: 5} ] In order to do that, I thought I found the magical solution, make things flat, using flatten function, it was not working ( this problem is just a piece of code in a lot of code ) and I was wondering why, i wasted some time to find that this the problem, it is not the behovior I am expecting, as you can see in the image, the first thing I have is an array containing an array having two objects, with flatten method, I was expecting an array of two objects, but I am getting what you see in the image : The code I have ttried is this : const expectedArray = R.flatten(myArrayOfArraysOfObjects); Full example : const singleTronconPoints = troncon => { return troncon.geometri_linestring; }; console.log('troncons : '); console.log(troncons); console.log('map troncons points'); console.log(map(singleTronconPoints, troncons)); console.log('flatten'); console.log(flatten(map(singleTronconPoints, troncons))); and this is full result : How can I solve that, is there another magical ( :P ) solution ( method ) to solve the problem ? Any help would be much appreciated.
Array.prototype.reduce() can also be an option: const arr =[ [{a: 1, b: 2},{a: 1, b: 3}], [{a: 5, b: 2},{a: 2, b: 5}] ] const expectedArray = arr.reduce((acc, array) => { acc.push(...array); return acc; }, []);
You can use array.flat let a = [ [{ a: 1, b: 2 }, { a: 1, b: 3 }], [{ a: 5, b: 2 }, { a: 2, b: 5 }] ]; let b = a.flat(); console.log(b) Alternatively you can use reduce and inside callback use forEach and puch items from the nested array to accumulator array let a = [ [{ a: 1, b: 2 }, { a: 1, b: 3 }], [{ a: 5, b: 2 }, { a: 2, b: 5 }] ]; let b = a.reduce((acc, curr) => { curr.forEach(item => acc.push(item)) return acc; }, []); console.log(b)
use reduce() + push which faster flat() method. refer this for to check performance. : https://jsbench.me/0ikcqa83ck/1 let arr = [ [{a: 1, b: 2},{a: 1, b: 3} ], [{a: 5, b: 2},{a: 2, b: 5}] ] console.log(arr.flat()) let flattenArr = arr.reduce((acc, val) => (acc.push(...val),acc), []) console.log(flattenArr);
Array.flat is the magical solution you are looking for ! var arr = [ [{a: 1, b: 2},{a: 1, b: 3} ], [{a: 5, b: 2},{a: 2, b: 5}] ] console.log(arr.flat())
Pivot/Transform Javascript array of objects
I need help with this please. I have the following array of objects: [ { name: "A", id: "q1", history: { "1:2:3": {a: 0, b: 0, c: 0}, "4:5:6": {a: 1, b: 1, c: 1}, "7:8:9": {a: 2, b: 2, c: 2} } }, { name: "B", id: "q2", history: { "1:2:3": {a: 3, b: 3, c: 3}, "4:5:6": {a: 4, b: 4, c: 4}, "7:8:9": {a: 5, b: 5, c: 5} } }, { name: "C", id: "q3", history: { "1:2:3": {a: 6, b: 6, c: 6}, "4:5:6": {a: 7, b: 7, c: 7}, "7:8:9": {a: 8, b: 8, c: 8} } } ] and I want to obtain the following result: { "1:2:3": { "q1": {a: 0, b: 0, c: 0}, "q2": {a: 3, b: 3, c: 3}, "q3": {a: 6, b: 6, c: 6} }, "4:5:6": { "q1": {a: 1, b: 1, c: 1}, "q2": {a: 4, b: 4, c: 4}, "q3": {a: 7, b: 7, c: 7} }, "7:8:9": { "q1": {a: 2, b: 2, c: 2}, "q2": {a: 5, b: 5, c: 5}, "q3": {a: 8, b: 8, c: 8} } } I want the result to be an object with 1:2:3, 4:5:6, 7:8:9 the keys. Some solutions please? Thank you! :)
It's a good example of how can one use reduce const data = [{ name: "A", id: "q1", history: { "1:2:3": { a: 0, b: 0, c: 0 }, "4:5:6": { a: 1, b: 1, c: 1 }, "7:8:9": { a: 2, b: 2, c: 2 } } }, { name: "B", id: "q2", history: { "1:2:3": { a: 3, b: 3, c: 3 }, "4:5:6": { a: 4, b: 4, c: 4 }, "7:8:9": { a: 5, b: 5, c: 5 } } }, { name: "C", id: "q3", history: { "1:2:3": { a: 6, b: 6, c: 6 }, "4:5:6": { a: 7, b: 7, c: 7 }, "7:8:9": { a: 8, b: 8, c: 8 } } } ] const result = data.reduce((acc, item) => { const keys = Object.keys(item.history); for (let key of keys) { acc[key] = { ...(acc[key] || {}), [item.id]: item.history[key] } } return acc; }, {}) console.log(result);
get list of object from array of objects using javascript
It is a simple javascript problem and i am unable to get my head through it const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ] let result = jsObjects.find(obj => { return obj.b === 6 }) console.log(result) i just want to console the entire list of 'b' rather than find a single variable 'b' which holds value 6 is there any way to do that
You can use Array.filter() instead of Array.find() const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ] let result = jsObjects.filter(obj => obj.b === 6) console.log(result) UPDATE If you want to take only one property, then you can use Array.map() const jsObjects = [ {a: 1, b: 2}, {a: 3, b: 4}, {a: 5, b: 6}, {a: 7, b: 8} ] let result = jsObjects.map(obj => obj.b) console.log(result);
If you still want them as objects in an array, you can update Harun's code like this: let myBs = []; let result = jsObjects.map(obj => myBs.push({b: obj.b})); console.log(myBs);```
Improve Array transformation in Javascript
Suppose I have a input array like the following var inputArray = [ {a: 1, b: 1, c: 1, d: 1, value: 1, rank: 1}, {a: 1, b: 1, c: 1, d: 1, value: 2, rank: 2}, {a: 1, b: 1, c: 1, d: 1, value: 3, rank: 3}, {a: 1, b: 1, c: 1, d: 1, value: 4, rank: 4}, {a: 1, b: 1, c: 1, d: 1, value: 5, rank: 5}, {a: 1, b: 2, c: 1, d: 1, value: 1, rank: 1}, {a: 1, b: 2, c: 1, d: 1, value: 2, rank: 2}, {a: 1, b: 2, c: 1, d: 1, value: 3, rank: 3}, {a: 1, b: 2, c: 1, d: 1, value: 4, rank: 4}, {a: 1, b: 2, c: 1, d: 1, value: 5, rank: 5} ] I want to transform my inputArray to the following outputArray var outputArray = [ { a: 1, b: 1, c: 1, d: 1, values:{ "1":{value: 1}, "2":{value: 2}, "3":{value: 3}, "4":{value: 4}, "5":{value: 5} } }, { a: 1, b: 2, c: 1, d: 1, values:{ "1":{value: 1}, "2":{value: 2}, "3":{value: 3}, "4":{value: 4}, "5":{value: 5} } } ] That means, I need create a dictionary for same property of a, b, c and d where the value of property rank is the key of the dictionary and the value of the dictionary is an object where the only property is value. We assume that the inputArray will not be sorted with respect to the combination of a, b, c and d. So, my approach is like this, (function(){ var inputArray = [ {a: 1, b: 1, c: 1, d: 1, value: 1, rank: 1}, {a: 1, b: 1, c: 1, d: 1, value: 2, rank: 2}, {a: 1, b: 1, c: 1, d: 1, value: 3, rank: 3}, {a: 1, b: 1, c: 1, d: 1, value: 4, rank: 4}, {a: 1, b: 1, c: 1, d: 1, value: 5, rank: 5}, {a: 1, b: 2, c: 1, d: 1, value: 1, rank: 1}, {a: 1, b: 2, c: 1, d: 1, value: 2, rank: 2}, {a: 1, b: 2, c: 1, d: 1, value: 3, rank: 3}, {a: 1, b: 2, c: 1, d: 1, value: 4, rank: 4}, {a: 1, b: 2, c: 1, d: 1, value: 5, rank: 5} ] var temp = inputArray.sort(function(valA, valB){ if(valA.a === valB.a){ if(valA.b === valB.b){ if(valA.c === valB.c){ return valA.d < valB.d; } return valA.c < valB.c; } return valA.b < valB.b; } return valA.a < valB.a; }); var outputArray = [], currentIndex = 0; for(var i = 0; i < inputArray.length; i++){ if(i > 0 && isConfigurationSame(inputArray[i], inputArray[i-1])){ outputArray[currentIndex-1].values[inputArray[i].rank] = { value: inputArray[i].value } } else{ outputArray.push(mapToOutputArrayObject(inputArray[i])); currentIndex++; } } console.log(outputArray); function isConfigurationSame(A, B) { return A.a === B.a && A.b === B.b && A.c === B.c && A.d === B.d; } function mapToOutputArrayObject(val){ var row = {}; row['a'] = val.a; row['b'] = val.b; row['c'] = val.c; row['d'] = val.d; row['values'] = {}; row.values[val.rank] = { value: val.value } return row; } }()); But the problem is, this thing is really getting more time if the length of input array is huge. This multi-criteria sorting also taking much time. Is there any better approach to accomplish the result more efficiently with less time? Thanks for your time and patience. UPDATE: The values of a, b, c and d can be integer or null.
You could create a hashtable and generate a unique key based on a, b, c and d: const hash = {}; for(const { a, b, c, d, value, rank } of array) { const key = JSON.stringify([a, b, c, d]); // generate a unique, but not random key if(hash[key]) { // check if it already exists, hash[key].values[rank] = value; // merge } else { hash[key] = { // create a new entry a, b, c, d, values: { [rank]: value }, }; } } const result = Object.values(hash); // turn the object into an array That is O(n), which is better as the time complexity of any .sort implementation (but it only works if a, b, c and d are serializable (like in this case)).
You could take a Map and an array of the grouping keys and collect the values for each group. var array = [{ a: 1, b: 1, c: 1, d: 1, value: 1, rank: 1 }, { a: 1, b: 1, c: 1, d: 1, value: 2, rank: 2 }, { a: 1, b: 1, c: 1, d: 1, value: 3, rank: 3 }, { a: 1, b: 1, c: 1, d: 1, value: 4, rank: 4 }, { a: 1, b: 1, c: 1, d: 1, value: 5, rank: 5 }, { a: 1, b: 2, c: 1, d: 1, value: 1, rank: 1 }, { a: 1, b: 2, c: 1, d: 1, value: 2, rank: 2 }, { a: 1, b: 2, c: 1, d: 1, value: 3, rank: 3 }, { a: 1, b: 2, c: 1, d: 1, value: 4, rank: 4 }, { a: 1, b: 2, c: 1, d: 1, value: 5, rank: 5 }], keys = ['a', 'b', 'c', 'd'], result = [], map = new Map; array.forEach(o => { var key = keys.map(k => o[k]).join('|'), temp = map.get(key); if (!temp) { map.set(key, temp = Object.assign(...keys.map(k => ({ [k]: o[k] })), { values: {} })); result.push(temp); } temp.values[o.rank] = { value: o.value }; }); console.log(result); .as-console-wrapper { max-height: 100% !important; top: 0; }
Here is a stab at it using Set, Map and a const method to build the Values object. var inputArray = [ {a: 1, b: 1, c: 1, d: 1, value: 1, rank: 1}, {a: 1, b: 1, c: 1, d: 1, value: 2, rank: 2}, {a: 1, b: 1, c: 1, d: 1, value: 3, rank: 3}, {a: 1, b: 1, c: 1, d: 1, value: 4, rank: 4}, {a: 1, b: 1, c: 1, d: 1, value: 5, rank: 5}, {a: 1, b: 2, c: 1, d: 1, value: 1, rank: 1}, {a: 1, b: 2, c: 1, d: 1, value: 2, rank: 2}, {a: 1, b: 2, c: 1, d: 1, value: 3, rank: 3}, {a: 1, b: 2, c: 1, d: 1, value: 4, rank: 4}, {a: 1, b: 2, c: 1, d: 1, value: 5, rank: 5} ]; const getValueObject = (a,b,c,d, arr) => { let obj = {}; arr.filter(i => i.a === a && i.b === b && i.c ===c && i.d === d) .forEach(item => obj[item.value] = item.rank); return obj; }; // Get a set based on the key a,b,c,d let newArray = [...new Set(inputArray.map(({a,b,c,d}) => `${a},${b},${c},${d}`))] .map(item => { let [a,b,c,d] = item.split(',').map(i => parseInt(i)); // filter and add return { a: a, b: b, c: c, d: d, values: getValueObject(a,b,c,d, inputArray) }; }); console.log(newArray);
Here is another option, first grouping by a, b, c and d. Then mapping over each group transforming the value and rank. var inputArray = [{a: 1, b: 1, c: 1, d: 1, value: 1, rank: 1}, {a: 1, b: 1, c: 1, d: 1, value: 2, rank: 2}, {a: 1, b: 1, c: 1, d: 1, value: 3, rank: 3}, {a: 1, b: 1, c: 1, d: 1, value: 4, rank: 4}, {a: 1, b: 1, c: 1, d: 1, value: 5, rank: 5}, {a: 1, b: 2, c: 1, d: 1, value: 1, rank: 1}, {a: 1, b: 2, c: 1, d: 1, value: 2, rank: 2}, {a: 1, b: 2, c: 1, d: 1, value: 3, rank: 3}, {a: 1, b: 2, c: 1, d: 1, value: 4, rank: 4}, {a: 1, b: 2, c: 1, d: 1, value: 5, rank: 5}]; function groupBy(array, callback) { return array.reduce((groups, item, ...args) => { const key = callback(item, ...args), group = groups[key] || (groups[key] = []); group.push(item); return groups; }, {}); }; console.log( Object .values( groupBy(inputArray, ({a, b, c, d}) => [a, b, c, d]) ) .map(group => { const {a, b, c, d} = group[0], values = {}; group.forEach(({value, rank}) => values[rank] = {value}); return {a, b, c, d, values}; }) );
Sorting each object's key in an array of objects [duplicate]
This question already has answers here: Sort JavaScript object by key (37 answers) Closed 4 years ago. I have the following array of object : var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{b: 2, a: 2},{d: 2, a: 2},{a: 2, b: 2}] and I would like to sort each object by keys so that it will become: var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{a: 2, b: 2},{a: 2, d: 2},{a: 2, b: 2}] I have tried the following code: //sort keys wordSpecsArr.forEach((obj) => { var ordered = {}; Object.keys(obj).sort().forEach((key) => { ordered[key] = obj[key] }); }); But it did not work. I would really appreciate any help
Try this. var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{b: 2, a: 2},{d: 2, a: 2},{a: 2, b: 2}] var wordSpecsArr = [{a: 2, b: 2},{a: 1, b: 1, c: 1, d: 1},{a: 2, b: 2},{a: 2, d: 2},{a: 2, b: 2}] //sort keys let op =[]; wordSpecsArr.forEach((obj) => { let ordered = {}; Object.keys(obj).sort().forEach((key) => { ordered[key] = obj[key] }); op.push(ordered) }); console.log(op)