简体   繁体   English

如何根据javascript中另一个数组的值映射和过滤值数组?

[英]How to map and filter array of values based on value from another array in javascript?

Here I am having two arrays.这里我有两个数组。

One array will hold the ids and another array holds the data.一个数组将保存 ID,另一个数组保存数据。 I want to map the values from the data by filtering with the value from the Id's array.我想通过使用 Id 数组中的值进行过滤来映射数据中的值。

eg: If 6 from the Ids array matches with data.id means I need to get the value inside the assets array.例如:如果 Ids 数组中的 6 与 data.id 匹配,则意味着我需要获取 assets 数组中的值。 I have done that solution.我已经完成了那个解决方案。 But my solution seems quite complicated to understand.但是我的解决方案似乎很难理解。

const ids = [
    6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,
    6, 5, 4, 4, 4
  ];
  
  const data = [
    {
      id: 10,
      assets: [
        {
          type: 'First Section',
          value: 60
        },
        {
          type: 'Second Section',
          value: 40
        }
      ]
    },
    {
      id: 11,
      assets: [
        {
          type: 'First Section',
          value: 65
        },
        {
          type: 'Second Section',
          value: 35
        }
      ]
    },
    {
      id: 12,
      assets: [
        {
          type: 'First Section',
          value: 70
        },
        {
          type: 'Second Section',
          value: 30
        }
      ]
    },
    {
      id: 13,
      assets: [
        {
          type: 'First Section',
          value: 75
        },
        {
          type: 'Second Section',
          value: 25
        }
      ]
    },
    {
      id: 14,
      assets: [
        {
          type: 'First Section',
          value: 80
        },
        {
          type: 'Second Section',
          value: 20
        }
      ]
    },
    {
      id: 15,
      assets: [
        {
          type: 'First Section',
          value: 85
        },
        {
          type: 'Second Section',
          value: 15
        }
      ]
    },
    {
      id: 16,
      assets: [
        {
          type: 'First Section',
          value: 90
        },
        {
          type: 'Second Section',
          value: 10
        }
      ]
    },
    {
      id: 2,
      assets: [
        {
          type: 'First Section',
          value: 20
        },
        {
          type: 'Second Section',
          value: 80
        }
      ]
    },
    {
      id: 3,
      assets: [
        {
          type: 'First Section',
          value: 25
        },
        {
          type: 'Second Section',
          value: 75
        }
      ]
    },
    {
      id: 4,
      assets: [
        {
          type: 'First Section',
          value: 30
        },
        {
          type: 'Second Section',
          value: 70
        }
      ]
    },
    {
      id: 5,
      assets: [
        {
          type: 'First Section',
          value: 35
        },
        {
          type: 'Second Section',
          value: 65
        }
      ]
    },
    {
      id: 6,
      assets: [
        {
          type: 'First Section',
          value: 40
        },
        {
          type: 'Second Section',
          value: 60
        }
      ]
    },
    {
      id: 7,
      assets: [
        {
          type: 'First Section',
          value: 45
        },
        {
          type: 'Second Section',
          value: 55
        }
      ]
    },
    {
      id: 8,
      assets: [
        {
          type: 'First Section',
          value: 50
        },
        {
          type: 'Second Section',
          value: 50
        }
      ]
    },
    {
      id: 9,
      assets: [
        {
          type: 'First Section',
          value: 55
        },
        {
          type: 'Second Section',
          value: 45
        }
      ]
    },
    {
      id: 1,
      assets: [
        {
          type: 'First Section',
          value: 15
        },
        {
          type: 'Second Section',
          value: 85
        }
      ]
    }
  ];

Here I need to get only the first section value that is inside of the assets array.在这里,我只需要获取 assets 数组中的第一个部分值。

I have done that by using the below code我已经通过使用下面的代码做到了这一点

 let res = ids.map((val) => data.filter(v => v.id === val)[0]).map(v => v.assets.filter(v => v.type === 'First Section')[0]).map(v => v.value)

console.log(res)

output输出

[
  40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  40, 40, 40, 40, 40, 40, 40, 40, 40, 40,
  40, 40, 40, 40, 35, 30, 30, 30
]

But is any way we can optimize the function furthermore.但是有什么方法可以进一步优化功能。 Please guide me on that请指导我

You are right in sensing that your algorithm, although working, is incredibly inefficient.您正确地感觉到您的算法虽然有效,但效率极低。 You are having to search through the entire data array for each item in the ids, which makes it an O(n^m) time complexity which will not scale well at all.您必须在整个数据数组中搜索 ids 中的每个项目,这使其成为 O(n^m) 时间复杂度,根本无法很好地扩展。

I think the best and most efficient way to do this would be to create a map of id -> first section value for each of your items in your data list.我认为最好和最有效的方法是为数据列表中的每个项目创建一个 id -> first section 值的映射。 From there, it is a very simple and fast lookup to grab the section value for any of your IDs.从那里,获取任何 ID 的部分值是一个非常简单且快速的查找。 This is how it can be done.这是可以做到的。 It only requires 1 pass through the data array, and 1 pass through the ids array - O(n + m), like so:它只需要 1 次通过数据数组,1 次通过 ids 数组 - O(n + m),如下所示:

 const ids = [ 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 5, 4, 4, 4 ]; const data = [{"id":10,"assets":[{"type":"First Section","value":60},{"type":"Second Section","value":40}]},{"id":11,"assets":[{"type":"First Section","value":65},{"type":"Second Section","value":35}]},{"id":12,"assets":[{"type":"First Section","value":70},{"type":"Second Section","value":30}]},{"id":13,"assets":[{"type":"First Section","value":75},{"type":"Second Section","value":25}]},{"id":14,"assets":[{"type":"First Section","value":80},{"type":"Second Section","value":20}]},{"id":15,"assets":[{"type":"First Section","value":85},{"type":"Second Section","value":15}]},{"id":16,"assets":[{"type":"First Section","value":90},{"type":"Second Section","value":10}]},{"id":2,"assets":[{"type":"First Section","value":20},{"type":"Second Section","value":80}]},{"id":3,"assets":[{"type":"First Section","value":25},{"type":"Second Section","value":75}]},{"id":4,"assets":[{"type":"First Section","value":30},{"type":"Second Section","value":70}]},{"id":5,"assets":[{"type":"First Section","value":35},{"type":"Second Section","value":65}]},{"id":6,"assets":[{"type":"First Section","value":40},{"type":"Second Section","value":60}]},{"id":7,"assets":[{"type":"First Section","value":45},{"type":"Second Section","value":55}]},{"id":8,"assets":[{"type":"First Section","value":50},{"type":"Second Section","value":50}]}]; // for each item in data, map id to first section value for easy lookup let sectionMap = data.reduce((res, curr) => { res[curr.id] = curr.assets.find(asset => asset.type === 'First Section').value; return res; }, {}); console.log(sectionMap); // map the id to its respective first section value with the lookup map we created let result = ids.map(id => sectionMap[id]); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM