Javascript 如何提取值并创建新对象,如下所示

Javascript 如何提取值并创建新对象,如下所示,javascript,Javascript,我拥有的对象如下: 我希望能够创建如下所示的新对象: const tagA={color:['red','green'],类型:{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] 让颜色=[] tags.forEach(t=>t.color.forEa

我拥有的对象如下:

我希望能够创建如下所示的新对象:

const tagA={color:['red','green'],类型:{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]
让颜色=[]
tags.forEach(t=>t.color.forEach(c=>colors.push(c)))
colors=colors.filter((c,i)=>colors.indexOf(c)==i)
设Color={}
colors.forEach(c=>Color[c]=tags.filter(t=>t.Color.includes(c)))

console.log(Color)
您可以使用下面的函数
getColorFilter
。了解更多关于和的信息

注意您需要将
对象
作为
getColorFilter({tagA,tagB,tagC})传递。或者您可以创建新对象,比如
let obj={tagA,tagB,tagC}
getColorFilter(obj)

函数getColorFilter(标记){
返回对象。条目(标记)
.flatMap(([key,val])=>val.color.map(c=>({tag:key,color:c})))
.减少((acc,x)=>{
acc[x.color]=acc[x.color]| | |[];
acc[x.color]。推送(x.tag);
返回acc;
}, {})
}
常数塔加={
颜色:[‘红色’、‘绿色’]
};
常数tagB={
颜色:[“蓝色”、“红色”]
};
常数tagC={
颜色:[“红色”、“绿色”、“黄色”]
};
const colorFilter=getColorFilter({tagA,tagB,tagC});

console.log(colorFilter)
看起来
颜色
就是您要寻找的对象。请澄清问题?到目前为止您尝试了什么?您无法将变量名(标识符)转换为这样的对象属性名。变量名是执行上下文的属性,您不能访问它,在函数调用中使用时也不能将其与值一起传递。在颜色对象中,每个颜色数组只能使用参数索引作为引用,而不能使用变量名。即使使用不同的对象名,也可以。与“const color”不同,使用不同的对象名称也可以,比如“const colorFilterObj”——@Noah Stahlconst colors对我来说是未知的,因为我需要能够通过tagA动态生成新对象(colorFilter),tagB&tagC@kartikgavara更新:将
const colors
替换为根据标记动态获取
颜色的流程。
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 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: tagA,
  tagB: tagB,
  tagC: tagC
};
let tagsTest = Object.keys(tags);
let colorFilter = {};
tagsTest.forEach(t => {
  tags[t].color.forEach(l => {
    if (colorFilter.hasOwnProperty(l)) {
      colorFilter[l].push(t);
      }
    else {
        colorFilter[l] = [t];
        }
    }
  )
});
console.log(colorFilter);

    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: tagA,
      tagB: tagB,
      tagC: tagC
    };
    let tagsTest = Object.keys(tags);
    let colorFilter = {};
    tagsTest.forEach(t => {
      tags[t].color.forEach(l => {
        if (colorFilter.hasOwnProperty(l)) {
          colorFilter[l].push(t);
          }
        else {
            colorFilter[l] = [t];
            }
        }
      )
    });
    console.log(colorFilter);