[英]How to get elements of common value present in all items of Array in JavaScript
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.如何根据在所有子行中具有公共subDataRowId来获取过滤列表。 Expected output looking for is as below -
预计 output 寻找如下 -
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.
我尝试像下面那样获取重复项并使用“rowList”子行对其进行迭代。 But not getting expected output.
但没有得到预期的 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 -小提琴链接 -
https://jsfiddle.net/guruling/e64atjds/74/ https://jsfiddle.net/guruling/e64atjds/74/
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.