如何找到某个值并将其替换并将所有值推送到新数组中?
How do I find a certain value and replace it in and push all the values in a new array?
我对 Javascript 有非常基本的了解。我正在编写一个插件来搜索一些预定义的颜色并用新颜色替换它们。到目前为止,我能够获得一个包含颜色、名称和样式的数组作为数组中的对象。
现在我想搜索某种颜色,例如 - eb40a2
并将其替换为 ffffff
并将此新值推送到新数组。
var ref=[];
....
ref.push({ name: styleName, color: styleColor, parent: styleParent, styles: styleId });
这给了我以下数组:
0: {name: "pink_theme/dark/fill/active", color: "eb40a2", styles: "S:5bebabedaa118ab6d135df59d7ba8861c05992a0,5:1"}
1: {name: "green_theme/light/fill/product", color: "c85200", styles: "S:380f15d999c08d9d97725c9915f479e52c1a343c,244:13"}
2: {name: "green_theme/light/fill/active", color: "00880d", styles: "S:b2aecc5927c659d7fba7d021b092bc90618043a5,189:0"}
3: {name: "green_theme/light/fill/product", color: "0081a0", styles: "S:1f28c38bfa1b10ca61aeaf706ac2a71fa6425950,244:11"}
您可以映射数组并有条件地更新您想要的颜色。检查下面的代码片段:
const arr = [
{
name: 'pink_theme/dark/fill/active',
color: 'eb40a2',
styles: 'S:5bebabedaa118ab6d135df59d7ba8861c05992a0,5:1',
},
{
name: 'green_theme/light/fill/product',
color: 'c85200',
styles: 'S:380f15d999c08d9d97725c9915f479e52c1a343c,244:13',
},
{
name: 'green_theme/light/fill/active',
color: '00880d',
styles: 'S:b2aecc5927c659d7fba7d021b092bc90618043a5,189:0',
},
{
name: 'green_theme/light/fill/product',
color: '0081a0',
styles: 'S:1f28c38bfa1b10ca61aeaf706ac2a71fa6425950,244:11',
},
];
const newArr = arr.map(el => el.color === "eb40a2" ? {...el, color: "ffffff"}: el)
console.log(newArr)
我对 Javascript 有非常基本的了解。我正在编写一个插件来搜索一些预定义的颜色并用新颜色替换它们。到目前为止,我能够获得一个包含颜色、名称和样式的数组作为数组中的对象。
现在我想搜索某种颜色,例如 - eb40a2
并将其替换为 ffffff
并将此新值推送到新数组。
var ref=[];
....
ref.push({ name: styleName, color: styleColor, parent: styleParent, styles: styleId });
这给了我以下数组:
0: {name: "pink_theme/dark/fill/active", color: "eb40a2", styles: "S:5bebabedaa118ab6d135df59d7ba8861c05992a0,5:1"}
1: {name: "green_theme/light/fill/product", color: "c85200", styles: "S:380f15d999c08d9d97725c9915f479e52c1a343c,244:13"}
2: {name: "green_theme/light/fill/active", color: "00880d", styles: "S:b2aecc5927c659d7fba7d021b092bc90618043a5,189:0"}
3: {name: "green_theme/light/fill/product", color: "0081a0", styles: "S:1f28c38bfa1b10ca61aeaf706ac2a71fa6425950,244:11"}
您可以映射数组并有条件地更新您想要的颜色。检查下面的代码片段:
const arr = [
{
name: 'pink_theme/dark/fill/active',
color: 'eb40a2',
styles: 'S:5bebabedaa118ab6d135df59d7ba8861c05992a0,5:1',
},
{
name: 'green_theme/light/fill/product',
color: 'c85200',
styles: 'S:380f15d999c08d9d97725c9915f479e52c1a343c,244:13',
},
{
name: 'green_theme/light/fill/active',
color: '00880d',
styles: 'S:b2aecc5927c659d7fba7d021b092bc90618043a5,189:0',
},
{
name: 'green_theme/light/fill/product',
color: '0081a0',
styles: 'S:1f28c38bfa1b10ca61aeaf706ac2a71fa6425950,244:11',
},
];
const newArr = arr.map(el => el.color === "eb40a2" ? {...el, color: "ffffff"}: el)
console.log(newArr)