如果值相等,则映射整个数组

时间:2019-01-10 09:03:38

标签: javascript arrays

我想映射一个数组,如果其中一个值等于变量,那么我将更改此数组中所有对象的isChecked键。例如

输入数组:

[
  [
    {
      "name": "size",
      "value": "XS",
      "isChecked": false
    },
    {
      "name": "colors",
      "value": "black",
      "isChecked": false
    }
  ],
  [
    {
      "name": "size",
      "value": "XXXL",
      "isChecked": false
    },
    {
      "name": "colors",
      "value": "brown",
      "isChecked": false
    }
  ],
  [
    {
      "name": "size",
      "value": "S",
      "isChecked": false
    },
    {
      "name": "colors",
      "value": "green",
      "isChecked": false
    }
  ]
]

输入值:黑色

输出:

[
  [
    {
      "name": "size",
      "value": "XS",
      "isChecked": true
    },
    {
      "name": "colors",
      "value": "black",
      "isChecked": true
    }
  ],
  [
    {
      "name": "size",
      "value": "XXXL",
      "isChecked": false
    },
    {
      "name": "colors",
      "value": "brown",
      "isChecked": false
    }
  ],
  [
    {
      "name": "size",
      "value": "S",
      "isChecked": false
    },
    {
      "name": "colors",
      "value": "green",
      "isChecked": false
    }
  ]
]

也许我应该使用一些回调吗?如何映射这个数组?我必须以某种方式回到我错过的价值。在这种情况下,如果找到数组中的值,则该数组中的所有元素都应标记为isChecked = true。

我现在有这样的东西:

this.allVariants.map((variant, key) => {

                return variant.map((opts, k) => {

                    if (opts.value == val && !opts.isChecked) {
                        let mapped = variant.map(op => op.isChecked = true);
                    } else {
                        let mapped = variant.map(op => op.isChecked = false);
                    }

                    return opts
                })
            })

2 个答案:

答案 0 :(得分:1)

您可以使用map方法并在some内部检查子数组中是否存在元素。

const data = [[{"name":"size","value":"XS","isChecked":false},{"name":"colors","value":"black","isChecked":false}],[{"name":"size","value":"XXXL","isChecked":false},{"name":"colors","value":"brown","isChecked":false}],[{"name":"size","value":"S","isChecked":false},{"name":"colors","value":"green","isChecked":false}]]

const res = data.map(arr => {
  const check = arr.some(({value}) => value == 'black');
  return check ? arr.map(e => ({...e, isChecked: true})) : arr
})

console.log(res)

答案 1 :(得分:0)

也许您正在寻找类似的东西?如您所见,有一个简单的map函数,该函数将根据输入到该函数的内容返回相关值的数组。只要find函数不返回null,它将简单地映射到提供的错误并更新相关对象。

它通过查看是否可以通过使用我实现的found函数在嵌套数组中找到提供的值来实现此目的,如果返回true,它将使用mutate函数。想法是您可能将来希望进一步更改给定对象的不同属性,因此为什么它具有专用功能。

我的答案与@NenadVracar相似,只是我将其分解为多个占用一行的函数。

let data = [[{name:"size",value:"XS",isChecked:!1},{name:"colors",value:"black",isChecked:!1}],[{name:"size",value:"XXXL",isChecked:!1},{name:"colors",value:"brown",isChecked:!1}],[{name:"size",value:"S",isChecked:!1},{name:"colors",value:"green",isChecked:!1}]];

// A function that states if relevant object with value exists.
let found = v => a => a.some(({value}) => value == v);

// A function used to return data that has been changed, specifically isChecked = true.
let mutate = a => a.map(i => ({...i, isChecked: true}));

// A function to return the desired array, takes an array and a value. 
let map = v => a => a.map(o => found(v)(o) ? mutate(o) : o);

console.log(map('black')(data));