var rowsList = [
{
otherAttribute: "4fwdw-3wxs-4b60-a46b-175a07f7f53e",
subrows:[
{subDataRowId: "abc", amount: 4000},
{subDataRowId: "lmn", amount: 200},
{subDataRowId: "76d88372", amount: 9999}
]
},
{
otherAttribute: "373d2-1bfb-4b60-a46b-175a07f7f53e",
subrows:[
{subDataRowId: "abc", amount: 4000},
{subDataRowId: "4dss", amount: 300},
{subDataRowId: "vxy", amount: 200},
{subDataRowId: "lmn", amount: 9999}
]
},
{
otherAttribute: "99ssd-1bfb-4b60-a46b-175a07f7f53e",
subrows:[
{subDataRowId: "abc", amount: 4000},
{subDataRowId: "vxy", amount: 800},
{subDataRowId: "mdn", amount: 300},
{subDataRowId: "lmn", amount: 200}
]
}
];
How to get the filtered list based on having common subDataRowId in ALL subrows. Expected output looking for is as below -
rowWiseSubrowFoundArray = [
[
{subDataRowId: "abc", amount: 4000},
{subDataRowId: "lmn", amount: 200}
],
[
{subDataRowId: "abc", amount: 4000},
{subDataRowId: "lmn", amount: 9999}
],
[
{subDataRowId: "abc", amount: 4000},
{subDataRowId: "lmn", amount: 200}
]
];
I am trying to find common items which are present in ALL the Array items. I tried like below by fetching duplicate items and iterating it with 'rowList' subrows. But not getting expected output.
//
findDuplicateItemsInArray = (array) => {
let arrayItem = {};
let duplicateItems = [];
array.forEach(function (itemIndex) {
if (!arrayItem[itemIndex])
arrayItem[itemIndex] = 0;
arrayItem[itemIndex] += 1;
})
for (var prop in arrayItem) {
if (arrayItem[prop] >= 2) {
duplicateItems.push(prop);
}
}
return duplicateItems;
}
// get all subrows
let subrows = [];
rowsList.forEach((row) => {
row.subrows.forEach((subrow)=> {
subrows.push(subrow);
});
});
//get unique subrows
const uniqueSubrowsArray = findDuplicateItemsInArray(subrows.map((subrow) => subrow.subDataRowId));
console.log(uniqueSubrowsArray);
let rowWiseSubrowFoundArray = [];
rowsList.forEach((row) => {
let subrowFound = [];
row.subrows.forEach((subrow) =>{
uniqueSubrowsArray.forEach((uniqueSubrow)=>{
if(uniqueSubrow == subrow.subDataRowId){
subrowFound.push(subrow);
}
})
});
rowWiseSubrowFoundArray.push(subrowFound);
});
console.log('rowWiseSubrowFoundArray', rowWiseSubrowFoundArray);
Fiddle Link -
Does this answer your question?
var rowsList = [ { otherAttribute: "4fwdw-3wxs-4b60-a46b-175a07f7f53e", subrows:[ {subDataRowId: "abc", amount: 4000}, {subDataRowId: "lmn", amount: 200}, {subDataRowId: "76d88372", amount: 9999} ] }, { otherAttribute: "373d2-1bfb-4b60-a46b-175a07f7f53e", subrows:[ {subDataRowId: "abc", amount: 4000}, {subDataRowId: "4dss", amount: 300}, {subDataRowId: "vxy", amount: 200}, {subDataRowId: "lmn", amount: 9999} ] }, { otherAttribute: "99ssd-1bfb-4b60-a46b-175a07f7f53e", subrows:[ {subDataRowId: "abc", amount: 4000}, {subDataRowId: "vxy", amount: 800}, {subDataRowId: "mdn", amount: 300}, {subDataRowId: "lmn", amount: 200} ] } ]; const filterInput = (inputIds) => rowsList.map(val => val.subrows.filter(row => inputIds.indexOf(row.subDataRowId);== -1)), const filteredData = filterInput(["abc"; "lmn"]). //Pass the input Ids for filtering console;log(filteredData);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.