根据数组 2 中的值设置数组 1 中对象的属性

发布于 2025-01-10 07:53:50 字数 1236 浏览 0 评论 0原文

我这里有两个数组。数组 1 是由一些对象组成的数组数组,如下所示:

var house = [
  [
    { x: 50, y: 50, selected: false },
    { x: 150, y: 50, selected: false },
    { x: 250, y: 50, selected: false },
    { x: 350, y: 50, selected: false },
    { x: 450, y: 50, selected: false },
    { x: 550, y: 50, selected: false },
    { x: 650, y: 50, selected: false },
    { x: 750, y: 50, selected: false },
    { x: 850, y: 50, selected: false },
    { x: 950, y: 50, selected: false },
  ],
  [
    { x: 50, y: 130, selected: false },
    { x: 150, y: 130, selected: false },
    { x: 250, y: 130, selected: false },
    { x: 350, y: 130, selected: false },
    { x: 450, y: 130, selected: false },
    { x: 550, y: 130, selected: false },
    { x: 650, y: 130, selected: false },
    { x: 750, y: 130, selected: false },
    { x: 850, y: 130, selected: false },
    { x: 950, y: 130, selected: false },
  ]
]

数组 2 由一些布尔值组成

var isSelected = [
  false,
  false,
  false,
  false,
  false,
  false,
  true,
  false,
  false,
  false,
  true,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  true,
  false
]

我如何迭代这些数组,以便如果数组 2 中的任何元素的值为 true,那么我必须设置数组 1 中相应数组中对象的属性“selected”也设置为 true 吗?

I have two arrays here. Array 1 is an array of arrays consisting of some objects as follows:

var house = [
  [
    { x: 50, y: 50, selected: false },
    { x: 150, y: 50, selected: false },
    { x: 250, y: 50, selected: false },
    { x: 350, y: 50, selected: false },
    { x: 450, y: 50, selected: false },
    { x: 550, y: 50, selected: false },
    { x: 650, y: 50, selected: false },
    { x: 750, y: 50, selected: false },
    { x: 850, y: 50, selected: false },
    { x: 950, y: 50, selected: false },
  ],
  [
    { x: 50, y: 130, selected: false },
    { x: 150, y: 130, selected: false },
    { x: 250, y: 130, selected: false },
    { x: 350, y: 130, selected: false },
    { x: 450, y: 130, selected: false },
    { x: 550, y: 130, selected: false },
    { x: 650, y: 130, selected: false },
    { x: 750, y: 130, selected: false },
    { x: 850, y: 130, selected: false },
    { x: 950, y: 130, selected: false },
  ]
]

And array 2 consisting of some boolean values

var isSelected = [
  false,
  false,
  false,
  false,
  false,
  false,
  true,
  false,
  false,
  false,
  true,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  true,
  false
]

How can I iterate over these arrays so that if the value of any element in array 2 is true, then I must set the value of the property "selected" of the object in the corresponding array in array 1 to true as well?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

别再吹冷风 2025-01-17 07:53:50
const isSelected = [
  false,
  false,
  false,
  false,
  false,
  false,
  true,
  false,
  false,
  false,
  true,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  true,
  false
]

let  house = [
  [
    { x: 50, y: 50, selected: false },
    { x: 150, y: 50, selected: false },
    { x: 250, y: 50, selected: false },
    { x: 350, y: 50, selected: false },
    { x: 450, y: 50, selected: false },
    { x: 550, y: 50, selected: false },
    { x: 650, y: 50, selected: false },
    { x: 750, y: 50, selected: false },
    { x: 850, y: 50, selected: false },
    { x: 950, y: 50, selected: false },
  ],
  [
    { x: 50, y: 130, selected: false },
    { x: 150, y: 130, selected: false },
    { x: 250, y: 130, selected: false },
    { x: 350, y: 130, selected: false },
    { x: 450, y: 130, selected: false },
    { x: 550, y: 130, selected: false },
    { x: 650, y: 130, selected: false },
    { x: 750, y: 130, selected: false },
    { x: 850, y: 130, selected: false },
    { x: 950, y: 130, selected: false },
  ]
]

const res = []
house.forEach(el => 
  res.push(
    el.map(({selected, ...rest}, i) => ({ selected: isSelected[i], ...rest}))
))  


console.log(res)

const isSelected = [
  false,
  false,
  false,
  false,
  false,
  false,
  true,
  false,
  false,
  false,
  true,
  false,
  false,
  false,
  false,
  false,
  false,
  false,
  true,
  false
]

let  house = [
  [
    { x: 50, y: 50, selected: false },
    { x: 150, y: 50, selected: false },
    { x: 250, y: 50, selected: false },
    { x: 350, y: 50, selected: false },
    { x: 450, y: 50, selected: false },
    { x: 550, y: 50, selected: false },
    { x: 650, y: 50, selected: false },
    { x: 750, y: 50, selected: false },
    { x: 850, y: 50, selected: false },
    { x: 950, y: 50, selected: false },
  ],
  [
    { x: 50, y: 130, selected: false },
    { x: 150, y: 130, selected: false },
    { x: 250, y: 130, selected: false },
    { x: 350, y: 130, selected: false },
    { x: 450, y: 130, selected: false },
    { x: 550, y: 130, selected: false },
    { x: 650, y: 130, selected: false },
    { x: 750, y: 130, selected: false },
    { x: 850, y: 130, selected: false },
    { x: 950, y: 130, selected: false },
  ]
]

const res = []
house.forEach(el => 
  res.push(
    el.map(({selected, ...rest}, i) => ({ selected: isSelected[i], ...rest}))
))  


console.log(res)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文