json 获取JavaScript中其他数组内部的几个数组的差异

mv1qrgav  于 2023-11-20  发布在  Java
关注(0)|答案(4)|浏览(97)

我有下面的json,从中读取数据:

[
  {
    "id": 1,
    "brand":"Mike",
    "cycle": "C1",
    "sides": {
      "1": {"sideName":"A",
          "active": false},
      "2": {"sideName":"B",
          "active": false},
      "3": {"sideName":"C",
          "active": true}
    }
  },
  {
    "id": 2,
    "brand":"Luis",
    "cycle": "C2",
    "sides": {
      "1": {"sideName":"A",
          "active": false},
      "2": {"sideName":"B",
          "active": false},
      "3": {"sideName":"C",
          "active": true},
      "4": {"sideName":"D",
          "active": true}
    }
  },
  {
    "id": 3,
    "brand":"Lucas",
    "cycle": "Z1",
    "sides": {
      "1": {"sideName":"H",
          "active": false},
      "2": {"sideName":"U",
          "active": false},
      "3": {"sideName":"L",
          "active": true},
      "4": {"sideName":"A",
          "active": true}
    }
  },
  {
    "id": 4,
    "brand":"Edd",
    "cycle": "R1",
    "sides": {
      "1": {"sideName":"A",
          "active": false},
      "2": {"sideName":"B",
          "active": false},
      "3": {"sideName":"C",
          "active": true},
      "4": {"sideName":"Q",
          "active": true}
    }
  }
]

字符串
我需要保留一个数组,其中包含所有常见的'sidenames',而不重复它们。我使用react jsx,但纯js解决方案对我来说也是可行的。示例:

结果= [“A”、“B”、“C”、“D”、“H”、“U”、“L”、“Q”]

s1ag04yj

s1ag04yj1#

使用Set。set是唯一项的集合:

const items = [
  {
    "id": 1,
    "brand":"Mike",
    "cycle": "C1",
    "sides": {
      "1": {"sideName":"A",
          "active": false},
      "2": {"sideName":"B",
          "active": false},
      "3": {"sideName":"C",
          "active": true}
    }
  },
  {
    "id": 2,
    "brand":"Luis",
    "cycle": "C2",
    "sides": {
      "1": {"sideName":"A",
          "active": false},
      "2": {"sideName":"B",
          "active": false},
      "3": {"sideName":"C",
          "active": true},
      "4": {"sideName":"D",
          "active": true}
    }
  },
  {
    "id": 3,
    "brand":"Lucas",
    "cycle": "Z1",
    "sides": {
      "1": {"sideName":"H",
          "active": false},
      "2": {"sideName":"U",
          "active": false},
      "3": {"sideName":"L",
          "active": true},
      "4": {"sideName":"A",
          "active": true}
    }
  },
  {
    "id": 4,
    "brand":"Edd",
    "cycle": "R1",
    "sides": {
      "1": {"sideName":"A",
          "active": false},
      "2": {"sideName":"B",
          "active": false},
      "3": {"sideName":"C",
          "active": true},
      "4": {"sideName":"Q",
          "active": true}
    }
  }
]

const uniqueSideNames = new Set();

for (const item of items) {
  for (const side of Object.values(item.sides)) {
    uniqueSideNames.add(side.sideName);
  }
}

console.log([...uniqueSideNames]);

字符串

qrjkbowd

qrjkbowd2#

一种选择是首先将列表缩减为您需要的项目,然后将它们flatMap为一个扁平数组。
items.flatMap(item => Object.values(item.sides).map(side => side.sideName))
这将给你一个给予 allsideNames的扁平列表,包括重复的。
Set数据结构是一种只允许唯一项的数据结构,因此将所有这些项添加到一个集合中,然后将它们转换回数组将为您进行重复数据删除。
这可能看起来像:

Array.from(new Set(items.flatMap(item => 
  Object.values(item.sides)
        .map(side => side.sideName)))
);

字符串

bnl4lu3b

bnl4lu3b3#

const arr = [{
    "id": 1,
    "brand": "Mike",
    "cycle": "C1",
    "sides": {
      "1": {
        "sideName": "A",
        "active": false
      },
      "2": {
        "sideName": "B",
        "active": false
      },
      "3": {
        "sideName": "C",
        "active": true
      }
    }
  },
  {
    "id": 2,
    "brand": "Luis",
    "cycle": "C2",
    "sides": {
      "1": {
        "sideName": "A",
        "active": false
      },
      "2": {
        "sideName": "B",
        "active": false
      },
      "3": {
        "sideName": "C",
        "active": true
      },
      "4": {
        "sideName": "D",
        "active": true
      }
    }
  },
  {
    "id": 3,
    "brand": "Lucas",
    "cycle": "Z1",
    "sides": {
      "1": {
        "sideName": "H",
        "active": false
      },
      "2": {
        "sideName": "U",
        "active": false
      },
      "3": {
        "sideName": "L",
        "active": true
      },
      "4": {
        "sideName": "A",
        "active": true
      }
    }
  },
  {
    "id": 4,
    "brand": "Edd",
    "cycle": "R1",
    "sides": {
      "1": {
        "sideName": "A",
        "active": false
      },
      "2": {
        "sideName": "B",
        "active": false
      },
      "3": {
        "sideName": "C",
        "active": true
      },
      "4": {
        "sideName": "Q",
        "active": true
      }
    }
  }
]

const result = []

arr.forEach(({
  sides
}) => {
  for (const key in sides) {
    if (!result.includes(sides[key].sideName)) result.push(sides[key].sideName)
  }
})

console.log(result)

字符串

brtdzjyr

brtdzjyr4#

将对象区分为它们的sideName后,可以使用new Set创建一个新数组。它设置的目的是创建没有重复值的数组。

[...new Set(items.flatMap(item=> Object.values(item.sides)).map(eachSide=> eachSide.sideName))]

字符串

输出:

['A', 'B', 'C', 'D', 'H', 'U', 'L', 'Q']

相关问题