I have a nested array ,i want to push new item to nested array.
FoodNode= [
{
name: 'Fruit',
parent:'root',
children: [
{name: 'Apple',parent:'Fruit'},
{name: 'Banana',parent:'Fruit'},
{name: 'Fruit loops',parent:'Fruit'},
]
}, {
name: 'Vegetables',
parent:'root',
children: [
{
name: 'Green',
parent:'Vegetables',
children: [
]
}, {
name: 'Orange',
parent:'Vegetables',
children: [
]
},
]
},
];
Now i want to add it to childrens of green.
this.fileElements.filter(res => {
if (res.parent === 'root') {
this.fileList.push(res);
} else {
this.fileList.filter((search, index) => {
if (search.name === res.parent) {
console.log(typeof search.children === 'undefined');
search.children.push(res);
}
});
}
});
this code push the item to 1 level only (like immediate children),bases on parent value Im pushing element to the children.I'm using mat-tree so I need same structure.please help
Hope it helps.
this.FoodNode.forEach(e => {
if (e.name == "Vegetables") {
let abcd = e.children;
abcd.forEach(e1 => {
if (e1.name === "Green") {
let data = [
{ name: "Apple" },
{ name: "Banana" },
{ name: "Fruit loops" }
];
data.forEach(e2 => {
e1["children"].push(e2);
});
}
});
}
});
Related
I have an array of ids ['id1', 'id3'].
I also have an array of items:
[
{
children: [{
children: [{
id: "id1", //This is value I need to find
status: { state: false}, //this is value I need to change
}],
}],
},
{
children: [{
children: [{
id: "id2",
status: { state: false},
}],
}],
},
{
children: [{
children: [{
id: "id3",
status: { state: false},
}],
}],
},
]
My goal is to find every item by id from first array, and change attribute state, then return all items having included those I have changed.
This was my try, but it returns all items again, also Im not sure how to change the attribute.
items.filter(item =>
item.children.map(child =>
child.children.map(object =>
idsArray.map(id => id === object.id)
)))
I think you can use recursive function something like below :
let ids = ["id1", "id2"];
let arrayOfItems = [
{
children: [
{
children: [
{
id: "id1",
status: {
state: false
}
}
]
}
]
},
{
children: [
{
children: [
{
id: "id2",
status: {
state: false
}
}
]
}
]
},
{
children: [
{
children: [
{
id: "id3",
status: {
state: false
}
}
]
}
]
}
];
function changeStatus(arrayOfItems, ids) {
return arrayOfItems.map((e) => {
if (e.id && ids.includes(e.id)) {
return { ...e, status: { state: true } };
} else if (e.children) {
return { ...e, children: changeStatus(e.children, ids) };
} else {
return { ...e };
}
});
}
console.log(changeStatus(arrayOfItems,ids));
I have an array of JSONs called products and another one called deletedProducts.
I want to filter those products that are not in deletedProducts.
Example:
products = [
{
id: 1,
name: 'Box'
},
{
id: 2,
name: 'Lamp'
}
]
deletedProducts = [
{
id: 1,
name: 'Box'
}
]
Result should be like this:
result = [
{
id: 2,
name: 'Lamp'
}
]
try filter and find methods :
let result =products.filter(prod=>{
return !deletedProducts.find(dprod=>{
return dprod.id===prod.id;
})
})
let products = [{
id: 1,
name: 'Box'
},
{
id: 2,
name: 'Lamp'
}
]
let deletedProducts = [{
id: 1,
name: 'Box'
}]
let result = products.filter(prod => {
return !deletedProducts.find(dprod => {
return dprod.id === prod.id;
})
})
console.log(result)
try this comparer function and filter. (reference by array of element "id" on this exmaple)
let products = [
{
id: 1,
name: 'Box'
},
{
id: 2,
name: 'Lamp'
}
]
let deletedProducts = [
{
id: 1,
name: 'Box'
}
]
function comparer(otherArray){
return function (current) {
return otherArray.filter(function(other) {
return other.id === current.id
}).length===0;
}
}
var result=products.filter(comparer(deletedProducts ));
console.log(result);
Here is what I have
[
{
typeProgramId: {
name: 'type1',
},
title: 'test1',
},
{
typeProgramId: {
name: 'type1',
},
subTypeProgramId: [{
name: 'sub1',
}],
title: 'test2',
},
{
typeProgramId: {
name: 'type2',
},
title: 'test3',
},
{
typeProgramId: {
name: 'type2',
},
subTypeProgramId: {
name: 'sub2',
},
title: 'test4',
}
]
First I want to group typeProgramId if the title have the same typeProgramId I want to push title into array by each typeProgramId but If the data have typeProgramId and subTypeProgram Id I want to group subtypeProgramId in typeProgramId too. if not empty subtypeProgramId I want to push it in array title inside subtypeProgram Id. I try to use lodash groupBy and many way but it still did not work.
Here is what I want
{
typeProgramId: [{
name: 'type1',
title: [
'test1',
],
subTypeProgramId: {
name: sub1,
title: [
'test2'
]
}
}, {
name: 'type2',
title: [
'test3',
],
subTypeProgramId: [{
name: sub1,
title: [
'test4'
]
}
}]
}
what I do now
let result = _.groupBy(getProgram, function(data) {
return data.typeProgramId
})
result = _.map(result, function(group, data) {
// I think in here I must groupBy subTypeProgramId again
// the data return all string not object after group
return {
typeProgramId: data,
titile: group,
}
})
Please check the below code. I have used reduce function of Array. It produces the expected result.
function updateMem(mem, prgIndex, val){
if(prgIndex < 0) {
mem.typeProgramId.push({});
prgIndex = mem.typeProgramId.length - 1;
}
mem.typeProgramId[prgIndex].name = val.typeProgramId.name;
if(val.subTypeProgramId){
mem.typeProgramId[prgIndex].subTypeProgramId = Object.assign({}, mem.typeProgramId[prgIndex].subTypeProgramId || {}, {"name" : val.subTypeProgramId.name, "title": []});
mem.typeProgramId[prgIndex].subTypeProgramId.title.push(val.title);
} else {
mem.typeProgramId[prgIndex].title = (mem.typeProgramId[prgIndex].title ? mem.typeProgramId[prgIndex].title : []);
mem.typeProgramId[prgIndex].title.push(val.title);
}
};
arr.reduce((mem, val) => {
var prgIndex = mem.typeProgramId.findIndex((p) => p.name === val.typeProgramId.name);
updateMem(mem, prgIndex, val);
return mem;
}, {typeProgramId: []});
arr1 = [
{
name: "will",
value: "1"
},
{
name: "nelson",
value: "3"
}
];
and
arr2 = [
{
name: "will",
value: 1,
submenu: [
{
name: "ralph",
value: 2
}
]
}
]
then remove ralph from the second array. i already created a function who do it but just check the first element and not verify the submenu.
comparador(arrSecundario) {
return (arrAtual) => {
return arrSecundario.filter(function (other) {
return other.value === arrAtual.value;
}).length !== 0;
};
}
this.arr2.filter(this.comparador(this.arr1));
Need to map array after filter. You can do like this and can also add "prop === 'submenu'" for specifically checking submenu inner array only.
var arr1 = [
{
name: "will",
value: "1"
},
{
name: "nelson",
value: "3"
}
];
var arr2 = [
{
name: "will",
value: 1,
submenu: [
{
name: "ralph",
value: 2
},
// {
// name: "nelson",
// value: 3
// }
]
}
];
function filterComparador(arrSecundario) {
return (arrAtual) => {
return arrSecundario.filter((other) => {
return other.value == arrAtual.value;
}).length != 0;
};
}
function mapComparador(arrSecundario) {
return (arrAtual) => {
Object.keys(arrAtual).forEach((prop) => {
if (Array.isArray(arrAtual[prop])) {
let propValue = arrAtual[prop].filter(this.filterComparador(arrSecundario));
if (propValue.length > 0) {
arrAtual[prop] = propValue.map(this.mapComparador(this.arrSecundario));
} else {
delete arrAtual[prop];
}
}
});
return arrAtual;
};
}
var manipulatedArray = this.arr2.filter(this.filterComparador(this.arr1))
.map(this.mapComparador(this.arr1));
console.log(manipulatedArray);
I need to access properties of a JavaScript object array and convert it to a new array. The array takes a form like this.
Company = [
{
name:"A",
items:[
{ name:"AA",
items:[
{ name:"AAA",
items:[...]
},
{ name:"AAB",
items:[...]
}
]
},
{ name:"AB",items:[{},{}]}
]
},
{
name:"B", items:[{ name:"BA",items:[{...},{...}]},{ name:"BB",items:[{...},{...}]}]
},
{
name:"C", items:[{ name:"CA",items:[{...},{...}]},{ name:"CB",items:[{...},{...}]}]
}
........
];
I need to convert this to a structure like this.
Company = [
{ title: 'A',
children: [
{ title: 'AA',
children: [{ title: 'AAA',children: [..] }]
},
{ title: 'AB', children: [{ title: 'ABA',children: [..] }
]
},
{ title: 'B',
children: [
{ title: 'BA',
children: [{ title: 'BAA',children: [..] }]
},
{ title: 'BB', children: [{ title: 'BBA',children: [..] }
]
},
.........
]
I tried to use ES6 Map,Reducer,Filter and lodash to get the array converted but i couldn't. Any idea on how to achive this?. Here is what i have tried out so far
function checkitems(data){
if (typeof data.items !== "undefined" || data.items.length > 0) {
data.items.map(dept => {
return {
title: dept.name,
children: checkitems(dept)
};
});
}
return;
}
company.map((dept)=>{
return {
title:dept.name,
children: checkitems(dept)
}
});
You could recursively update the items like this:
const Company=[{name:"A",items:[{name:"AA",items:[{name:"AAA",},{name:"AAB",}]},{name:"AB"}]},{name:"B",items:[{name:"BA",items:[{name:"BAA",},{name:"BAB",}]},{name:"BB",}]},{name:"C",items:[{name:"CA",},{name:"CB",}]}];
function trasnform(array) {
return array.map(({ name , items }) => {
const obj = { title: name }
if(items)
obj.children = trasnform(items);
return obj;
})
}
console.log(trasnform(Company))
Using destructuring assignment and default arguments -
const transform = (arr = []) =>
arr.map(({ name, items = [] }) =>
({ title: name, children: transform(items) })
)
Decomposed using mutual recursion -
const transform1 = ({ name, items = [] }) =>
({ title: name, children: transform(items) })
const transform = (arr = []) =>
arr.map(transform1)