gpt4 book ai didi

javascript - 将对象数组插入到另一个对象中

转载 作者:行者123 更新时间:2023-11-28 11:44:06 25 4
gpt4 key购买 nike

我正在尝试将两个对象合并为一个,但我需要确保第一个对象数组的值和键成为另一个对象数组的键值,希望这是有意义的:

所以我有一个包含如下信息的 dataConstant:

export const tileData = [
{
title: 'JS CONS',
skill: 40,
visitTitle: 'Visit JS',
visitUrl: 'https://www.google.co.uk'
},
{
title: 'JS CONS',
skill: 50,
visitTitle: 'Visit JS',
visitUrl: 'https://www.google.co.uk'
}
]

我将此 const 导入到我的 React 组件中。在同一内部,我导入多个图像并将它们放入对象数组中,如下所示:

//the values of the objects keys are imported logos
getLogos(tileData) {
const logo = [{logo: js}, {logo: react}];
var combi = [...logo, ...i];
console.log(combi);
return combi;
}

结果是:

0: {logo: "/static/media/js.a672e76f.png"}
1: {logo: "/static/media/react.5513eea1.png"}
2: {title: "JS CONS", skill: 40, visitTitle: "Visit JS", visitUrl: "https://www.google.co.uk"}
3: {title: "JS CONS", skill: 50, visitTitle: "Visit JS", visitUrl: "https://www.google.co.uk"}

但我希望前两个元素 01 位于 34 内部

最佳答案

let logos = [{logo: "/static/media/js.a672e76f.png"},{logo: "/static/media/react.5513eea1.png"}]

let tileData = [{title: "JS CONS", skill: 40, visitTitle: "Visit JS", visitUrl: "https://www.google.co.uk"},{title: "JS CONS", skill: 50, visitTitle: "Visit JS", visitUrl: "https://www.google.co.uk"}]

您可以使用.map()来组合两个数组,如下所示:

let combinedArr = logos.map((item, index) => {
return {
...item,
...tileData[index]
}
})

结果:

(2) [{…}, {…}]
0:
logo: "/static/media/js.a672e76f.png"
skill: 40
title: "JS CONS"
visitTitle: "Visit JS"
visitUrl: "https://www.google.co.uk"
__proto__: Object
1:
logo: "/static/media/react.5513eea1.png"
skill: 50
title: "JS CONS"
visitTitle: "Visit JS"
visitUrl: "https://www.google.co.uk"
__proto__: Object
length: 2
__proto__: Array(0)

关于javascript - 将对象数组插入到另一个对象中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56567974/

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