【问题标题】:how to convert array using filter method from values of another array in javascript如何使用过滤器方法从javascript中另一个数组的值转换数组
【发布时间】:2020-08-19 06:16:46
【问题描述】:

我有这样的数组

const data: { "status" : ["aa","bb","cc","dd"],
                       ["ee","ff","gg","hh"],
                       ["ii","jj","kk","ll"] } 
const value=2;

还有一个这样的数组

const array2: { "name":"status", "data": ["cc","gg"] }

我想把array1转换成这个

const data: "status" : ["aa","bb","cc","dd"],
                      ["ee","ff","gg","hh"] 
  

这里的值是我从第二个数组中获取我必须过滤的数据的索引。 而在array1中,data有键值对,status是key,value是数组

我使用过滤方法。但我无法通过状态来过滤数组。

附:提前谢谢你

【问题讨论】:

  • 请展示您的尝试。
  • array1和array2实际上是数组吗?
  • 请不要只删除并重新发布the same question again。这是您第三次发布它。

标签: javascript arrays reactjs filter


【解决方案1】:

如果这是您要求的,我不肯定,但您似乎想将data.status 的数组修剪为仅包含array2.data 中至少一个值的数组。

要检查这一点,您可以在过滤器中使用some 数组方法并检查每个元素是否包含在array2.data 中。

const data = {
  "status": [
    ["aa", "bb", "cc", "dd"],
    ["ee", "ff", "gg", "hh"],
    ["ii", "jj", "kk", "ll"]
  ]
}

const array2 = {
  "name": "status",
  "data": ["cc", "gg"]
}

console.log('Before')
console.log(data.status)

data.status = data.status.filter(row => row.some(elem => array2.data.includes(elem)))

console.log('After')
console.log(data.status)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 2012-03-06
    • 2020-06-24
    • 2013-09-24
    • 1970-01-01
    • 2021-01-27
    • 2011-11-06
    相关资源
    最近更新 更多