gpt4 book ai didi

javascript - 如何提取值并创建一个新对象,如下所示

转载 作者:行者123 更新时间:2023-12-01 16:24:57 24 4
gpt4 key购买 nike

The Objects I have are as below:

const tagA = {
color: ['red', 'green'],
type: { a: 10, b:7}...
};
const tagB = {
color: ['blue', 'red'],
type: { b:54, z:10} ....
};
const tagC = {
color: ['red', 'green', 'yellow'],
type: { a: 13, b:17}...
};

I want to be able to create a new object like below:

const colorFilter = {
red: ['tagA', 'tagC'],
green: ['tagA', 'tagC'],
blue: ['tagB'],
yellow: ['tagC']
};

最佳答案

const tagA = {color: ['red', 'green'],type: {a: 10,b: 7}}
const tagB = {color: ['blue', 'red'],type: {b: 54,z: 10}}
const tagC = {color: ['red', 'green', 'yellow'],type: {a: 13,b: 17}}
const tags = [tagA,tagB,tagC]
let colors = []
tags.forEach(t=>t.color.forEach(c=>colors.push(c)))
colors = colors.filter((c,i)=>colors.indexOf(c)===i)
let Color = {}
colors.forEach(c=>Color[c]=tags.filter(t=>t.color.includes(c)))
console.log(Color)

关于javascript - 如何提取值并创建一个新对象,如下所示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63294284/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com