How to fetch the all objects from the array of array? [duplicate] - javascript

This question already has answers here:
JavaScript flattening an array of arrays of objects
(14 answers)
Closed 26 days ago.
I need to fetch all objects from the array of array , but i am unable to find out that, i there any array method from which we can find out the objects from the array of array.
const array1 = [
[{
id:1,
name:"test1"
},
{
id:2,
name:"test2"
},
{
id:3,
name:"test3"
},
],
[{
id:4,
name:"test4"
},
{
id:5,
name:"test5"
},
{
id:6,
name:"test6"
},
],
[{
id:7,
name:"test7"
},
{
id:8,
name:"test8"
},
{
id:9,
name:"test9"
},
],
]
i just need to that i can get the all objects in a array, using the array methods

Just uses the flat method.
The flat() method creates a new array with all sub-array elements
concatenated into it recursively up to the specified depth.
const array1 = [
[{
id: 1,
name: "test1",
},
{
id: 2,
name: "test2",
},
{
id: 3,
name: "test3",
},
],
[{
id: 4,
name: "test4",
},
{
id: 5,
name: "test5",
},
{
id: 6,
name: "test6",
},
],
[{
id: 7,
name: "test7",
},
{
id: 8,
name: "test8",
},
{
id: 9,
name: "test9",
},
],
];
let result = array1.flat()
console.log(result);

Related

How to keep from each parent only children by id from object

I am trying to keep only children from each parent if it matches a child's id from a given list of parent/child ids.
var parentIds = ['1','2','3'];
var childrenIds = ['2','3','5'];
Parent with id '1' should only have children with id '2', parent with id '2' should only have children with id '3' and parent with id '3' should only have children with id '5'. So, parent id matches children id in same position from both arrays.
Example of initial object:
var object = [
{
name: "parent1",
id: 1,
option_values: [
{
name: "child1",
id: 1
},
{
name: "child2",
id: 2
},
{
name: "child8",
id: 8
}
]
},
{
name: "parent2",
id: 2,
option_values: [
{
name: "child3",
id: 3
},
{
name: "child1",
id: 1
},
{
name: "child9",
id: 9
}
]
},
{
name: "parent3",
id: 3,
option_values: [
{
name: "child5",
id: 5
},
{
name: "child4",
id: 4
},
{
name: "child13",
id: 13
}
]
},
]
Expected result:
var result = [
{
name: "parent1",
id: 1,
option_values: [
{
name: "child2",
id: 2
}
]
},
{
name: "parent2",
id: 2,
option_values: [
{
name: "child3",
id: 3
}
]
},
{
name: "parent3",
id: 3,
option_values: [
{
name: "child5",
id: 5
}
]
},
]
Loop through the objects. Find the index of the parent ID in the parentIds array, then get the corresponding element of childIds. Then filter the option_values array using that child ID.
var object = [{
name: "parent1",
id: 1,
option_values: [{
name: "child1",
id: 1
},
{
name: "child2",
id: 2
},
{
name: "child8",
id: 8
}
]
},
{
name: "parent2",
id: 2,
option_values: [{
name: "child3",
id: 3
},
{
name: "child1",
id: 1
},
{
name: "child9",
id: 9
}
]
},
{
name: "parent3",
id: 3,
option_values: [{
name: "child5",
id: 5
},
{
name: "child4",
id: 4
},
{
name: "child13",
id: 13
}
]
},
];
var parentIds = ['1', '2', '3'];
var childrenIds = ['2', '3', '5'];
object.forEach(obj => {
let idIndex = parentIds.indexOf(String(obj.id));
if (idIndex != -1) {
let childId = parseInt(childrenIds[idIndex]);
obj.option_values = obj.option_values.filter(child => child.id == childId);
}
});
console.log(object);

How to remove the undefined/null from array inside the array in angular [duplicate]

This question already has answers here:
Remove all falsy values from an array
(26 answers)
Removing undefined values from Array
(14 answers)
Closed 8 months ago.
DATA = [{
application: [{
name: 'Room1'
},{
name: 'Room2'
},{
name: 'Room3'
},{
name: 'Room4'
},{
name: 'Room5'
}, , undefined, null, null],
name: 'Batch 1',
date: '2020-10-20'
}]
What I'm trying to do here is to remove the undefined/null from the array.
the undefined/null should be remove.
output should be like this:
[{
application: [{
name: 'Room1'
},{
name: 'Room2'
},{
name: 'Room3'
},{
name: 'Room4'
},{
name: 'Room5'
}],
name: 'Batch 1',
date: '2020-10-20'
}]
it will remove the null or undefined
You could use the Array.prototype.filter method to keep only !!item (i.e. not-null, not-undefined, not-empty, not-false and not-zero items).
let DATA = [{
application: [{
name: 'Room1'
}, {
name: 'Room2'
}, {
name: 'Room3'
}, {
name: 'Room4'
}, {
name: 'Room5'
}, , undefined, null, null],
name: 'Batch 1',
date: '2020-10-20'
}];
DATA[0].application = DATA[0].application.filter(item => !!item);
console.log(DATA);
DATA = [{
application: [{
name: 'Room1'
},{
name: 'Room2'
},{
name: 'Room3'
},{
name: 'Room4'
},{
name: 'Room5'
}, , undefined, null, null],
name: 'Batch 1',
date: '2020-10-20'
}]
DATA[0].application = DATA[0].application.filter(i=> i);
console.log(DATA)

How to set Object before Array in JS?

in my current angular project I have to change a JSON array so that it can be displayed in a tree. So that the tree can output the object, a nested array must be packed in an object.
Expected form / output:
this.nodes = [
{
id: 1,
name: 'root1',
children: [
{ id: 2, name: 'child1' },
{ id: 3, name: 'child2' }
]
}, ....
My current form:
[{
id: 1,
name: 'root1',
name2: [{
id: 2,
name: 'child1'
},
{
id: 3,
name: 'child2',
name3: [{
id: 3,
name: 'child2'
}],
},
]
}], ...
How can I cange my 'name2' or 'name3' to children?
I've got this fat one liner:
const rename = (array) => array.map(item => Object.fromEntries(Object.entries(item).map(([k, v]) => k.match(/name\d+/) ? ['children', rename(v)] : [k, v])));
const nodes = [{
id: 1,
name: 'root1',
name2: [
{
id: 2,
name: 'child1'
},
{
id: 3,
name: 'child2',
name3: [
{
id: 3,
name: 'child2'
}
],
},
]
}];
const rename = (array) => array.map(item => Object.fromEntries(Object.entries(item).map(([k, v]) => k.match(/name\d+/) ? ['children', rename(v)] : [k, v])));
console.log(JSON.stringify(rename(nodes), null, 4));
// [
// {
// "id": 1,
// "name": "root1",
// "children": [
// {
// "id": 2,
// "name": "child1"
// },
// {
// "id": 3,
// "name": "child2",
// "children": [
// {
// "id": 3,
// "name": "child2"
// }
// ]
// }
// ]
// }
// ]

Find Object inside an Array of nested Objects (Lodash / Javascript / Typescript) [duplicate]

This question already has answers here:
javascript - In an array of objects, returns objects where ANY value matches a specific string
(5 answers)
Closed 4 years ago.
Lets say have an Array of Objects that looks like this:
let example = [
{
children: [{
data: { id: 2, group: 1001, name: "Audi" },
}],
data: { id: 1, group: 1000, name: "Cars" }
},
{
children: [{
data: { id: 4, group: 1003, name: "Airbus A320" },
}],
data: { id: 3, group: 1002, name: "Planes" }
},
{
children: [{
data: { id: 6, group: 1005, name: "Departed" }
}],
data: { id: 5, group: 1006, name: "movies" }
}
]
In my application the User selects a Tablerow and i get the selected Row Information aka the 'data' object e.g.
{ id: 2, group: 1001, name: "Audi" }
Now i want to find that selected data object based on the Id in my Array using lodash or javascript/typescript.
How would i achive that? The children Array causes me problems.
EDIT: The Child of a Child should also be found.
{
children: [{
children: [{
data: {id : 7, group 1001, name: "A8"},
children: [{...}]
}],
data: { id: 2, group: 1001, name: "Audi" },
}],
data: { id: 1, group: 1000, name: "Cars" }
}
/** find by id data using recursion */
function findById(data, id) {
for (const datum of data) {
if (datum.data.id == id) return datum
if (datum.children) {
let result = findById(datum.children, id)
if (result) return result
}
}
}
let example = [{
children: [{
data: {
id: 2,
group: 1001,
name: "Audi"
},
}],
data: {
id: 1,
group: 1000,
name: "Cars"
}
},
{
children: [{
data: {
id: 4,
group: 1003,
name: "Airbus A320"
},
}],
data: {
id: 3,
group: 1002,
name: "Planes"
}
},
{
children: [{
data: {
id: 6,
group: 1005,
name: "Departed"
}
}],
data: {
id: 5,
group: 1006,
name: "movies"
}
}
]
console.log(findById(example, 2))

How to add object element in array based on condition

I have static array constant of objects something similar to below.
export const EMPLOYEES = [
{
id: 2,
name: ‘John’,
},
{
id: 3,
name: ‘Doe’,
},
{
id: 4,
name: ‘Bull’,
},
{
id: 5,
name: ‘Scott’,
},
];
Now I need to add the last element only based on if some condition is true. Some this like if isAmerican() is true.
Can somebody help me here how to add element based on the condition? Thanks.
You can do it using spread operator:
export const EMPLOYEES = [
{
id: 2,
name: "John",
},
{
id: 3,
name: "Doe",
},
{
id: 4,
name: "Bull",
},
{
id: 5,
name: "Scott",
},
... isAmerican() ? [{ id: 6, name: "Jemmy"}] : []
];
You should never modify (or try to modify) a constant. I can see two ways you can do this:
Create a pure function to return a new constant with the new object added to the array
Use a spread operator in the definition of the constant
Option 1: Pure function
function makeNewArray(array, objectToAppend, isAmerican) {
return isAmerican ? [...array, objectToAppend] : array
}
const EMPLOYEES = [
{
id: 2,
name: "John",
},
{
id: 3,
name: "Doe",
},
{
id: 4,
name: "Bull",
},
{
id: 5,
name: "Scott",
}
];
const arrayWithAmerican = makeNewArray(EMPLOYEES, { id: 6, name: "American Frank"}, true);
const arrayWithoutAmerican = makeNewArray(EMPLOYEES, { id: 6, name: "Not American Frank"}, false);
console.log(arrayWithAmerican);
console.log(arrayWithoutAmerican);
Option 2: Spread operator
function isAmerican(){
// generic code here.
return true;
}
const EMPLOYEES = [
{
id: 2,
name: "John",
},
{
id: 3,
name: "Doe",
},
{
id: 4,
name: "Bull",
},
{
id: 5,
name: "Scott",
},
... isAmerican() ? [{ id: 6, name: "American Frank"}] : []
];
If the condition will be fulfilled, simply push an object to your EMPLOYEES array:
let isAmerican = true;
const EMPLOYEES = [
{
id: 2,
name: "John",
},
{
id: 3,
name: "Doe",
},
{
id: 4,
name: "Bull",
},
{
id: 5,
name: "Scott",
},
];
if(isAmerican) {
EMPLOYEES.push({
id: 6,
name: "Frank"
})
}
console.log(EMPLOYEES)
Fiddle: https://jsfiddle.net/rqx35pLz/

Categories

Resources