I have an object like this:
var object = {
a: 5,
b: ["a", "b"],
c: {
a: 1,
b: 2
}
}
And I would like to be transformed to a flat one like this:
var output = {
a: 5,
b: "a, b",
"c.a": 1,
"c.b": 2
}
Is there any function already done about this?
I have a mongodb database that has hierarchy documents and I need them to be converted to a flat one. Thanks
Related
I have an array of object as below.
data: [ {col: ['amb', 1, 2],} , {col: ['bfg', 3, 4], },]
From above, I need to get array of array as below.
[ [{a: 'amb',b: [1], c: 'red'}, {a: 'amb',b: [2], c: 'orange'}],
[{a: 'bfg',b: [3], c: 'red'}, {a: 'bfg',b: [4], c: 'orange'}]
]
My attempt is as below.
let arrInner: Array<any> = []
let arrOuter: Array<Array<any>> = []
_.forEach(data, (item, i) => {
//create two objects redObj and orangeObj
redObj = {
a: item.col[0].toString(),
b: [item.col[1] as number],
c: 'red'
}
orangeObj = {
a: item.col[0].toString(),
b: [item.col[2] as number],
c: 'orange'
}
//put those two objects to array
arrInner.push(redObj)
arrInner.push(orangeObj)
//assign that array to another array
arrOuter[i] = arrInner
})
But when I print the arrOuter, it is not my expected output. Where I was wrong and how can I fix this?
You need to create a new arrInner each time through the forEach loop. Then push that onto arrOuter.
let arrOuter: Array <Array <any>> = []
_.forEach(data, (item, i) => {
//create two objects redObj and orangeObj
redObj = {
a: item.col[0].toString(),
b: [item.col[1] as number],
c: 'red'
}
orangeObj = {
a: item.col[0].toString(),
b: [item.col[2] as number],
c: 'orange'
}
//put those two objects to array
let arrInner = [redObj, orangeObj]
//assign that array to another array
arrOuter.push(arrInner)
})
I have an object like this:
const myObj = {
a: {
b: {
c: 1,
d: 2
},
f: {
z: 4,
u: 6
}
}
}
into this:
const myObj = [
{
c: 1,
d: 2,
},
{
z: 4,
u: 6,
}
]
I found this: How to recursively transform an array of nested objects into array of flat objects? but the original is an array of objects, and mine is an object itself.
You can traverse the values of the objects until you reach the leaves (objects with no values that are other objects).
const myObj = {
a: {
b: {
c: 1,
d: 2
},
f: {
z: 4,
u: 6
}
}
};
const flatObj = o => Object.values(o).some(x => x === Object(x)) ?
Object.values(o).flatMap(flatObj) : [o];
console.log(flatObj(myObj))
I am trying to remove these 2 properties from each object in the array while it's not working. Each object is inside an array. I mean The main array contains arrays of objects. Is there an easy way to solve it without using the map() twice? And how to return the main modified array in that case?
const modifiedItems = this.items.map(item => {
delete item.created,
delete item.removed
return item
})
Data looks like this:
this.items = [
[{ removed: 1, created: 1, a:2, b: 2, c: 3}]
[{ removed: 1, created: 1, a:2, b: 2, c: 3}, { removed: 1, created: 1, a:2, b: 2, c: 3},]
];
The above code doesn't work because it doesn't map to arrays inside the main array. How is the best to delete those properties from each object of all arrays located in the main array?
Instead of deleting, why not just return an object without the properties you want to remove.
You could destructure the properties you want to remove and then collect other properties in a variable using the rest parameters syntax. After this, you just need to return the variable which contains all the properties other than the ones you want to remove.
const modifiedItems = this.items.map(
({ created, removed, ...rest }) => rest
);
Following code snippet shows an example:
const arr = [
{ removed: 1, created: 1, a:2, b: 2, c: 3},
{ removed: 1, created: 1, a:2, b: 2, c: 3},
{ removed: 1, created: 1, a:2, b: 2, c: 3},
];
const modifiedItems = arr.map(
({ created, removed, ...rest }) => rest
);
console.log(modifiedItems);
Edit:
In your case, this.items is an array that contains nested arrays. So to remove the properties from the objects inside the nested arrays, you need to map over each nested array as well.
Following code snippet shows an example:
const items = [
[ { removed: 1, created: 1, a: 2, b: 2, c: 3 } ],
[
{ removed: 1, created: 1, a: 2, b: 2, c: 3 },
{ removed: 1, created: 1, a: 2, b: 2, c: 3}
]
];
const modifiedItems = items.map(subArr => {
return subArr.map(({ created, removed, ...rest }) => rest)
});
console.log(modifiedItems);
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())
Is there a simple way to achieve this with lodash?
_.something([{a: 3, b: 4}, {a: 3, b: 5}, {a: 10}], 'a')
=> { 3: [ {a: 3, b: 4}, {a: 3, b: 5 } ], 10: [{ a: 10 }]}
That is, group all the values that share the same key together as an array under that key.
You could use _.groupBy for grouping by a given key.
Creates an object composed of keys generated from the results of running each element of collection thru iteratee. The order of grouped values is determined by the order they occur in collection. The corresponding value of each key is an array of elements responsible for generating the key. The iteratee is invoked with one argument: (value).
console.log(_.groupBy([{ a: 3, b: 4 }, { a: 3, b: 5 }, { a: 10 }], 'a'));
.as-console-wrapper { max-height: 100% !important; top: 0; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>