简体   繁体   English

如何获取 JavaScript 中所有数组项中存在的公共值元素

[英]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.

相关问题 如何在 JavaScript 中的 Object 数组中获得共同值 - How to get common value in Object Array in JavaScript 如何识别一个数组的所有元素是否存在于另一个数组中 - Javascript How to identify if all elements of one array are present in another 查找对象数组的所有常见元素-Javascript - Find all common elements of an array of objects - Javascript 如何获取所有LI UL元素ID值并将它们放置在JavaScript数组中? - How do I get all the LI UL elements ID value and place them in a JavaScript array? 如何使用在Javascript中具有共同值的另一个数组元素的索引获取数组? - How to use get an array of indexes of another array elements that has common values in Javascript? 如何使用jQuery将节点的所有子节点作为javascript中的数组项 - how to get all the children of a node as items of an array in javascript using jQuery Javascript-如何通过将数组的所有元素相乘来返回正确的值? - Javascript - How to return the correct value with the multiplication of all the elements of an array? 如何获取多个div中的所有数组元素值 - How to get all array elements value in multiple div 如何获取存在于数组属性中的值? - How to get value that is present in attribute of an array? 获取JavaScript数组中所有元素的列表 - Get list of all elements in a JavaScript array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM