gpt4 book ai didi

javascript - 如何在javascript中将属性与内部对象数组映射

转载 作者:行者123 更新时间:2023-11-28 17:03:36 26 4
gpt4 key购买 nike

我只是想在开始下一个函数之前使用 map 完全返回我的数组。但我在安慰它时收到不完整的数组

这是我的示例数组:

data = [{
"a": 1,
"b": 5,
"c": 9,
"d": 1,
"e": 4,
"f": 44,
"g": 23,
"h": 12,
"i": 22,
"j": 23,
"k": [{
"a": 1,
"b": 5,
"c": 9,
"d": 1
}, {
"a": 1,
"b": 5,
"c": 9,
"d": 1
}]
},
{
"a": 2,
"b": 6,
"c": 10,
"d": 1,
"e": 4,
"f": 44,
"g": 23,
"h": 12,
"i": 22,
"j": 23,
"k": [{
"a": 1,
"b": 5,
"c": 9,
"d": 1
}, {
"a": 1,
"b": 5,
"c": 9,
"d": 1
}]
},
{
"a": 3,
"b": 7,
"c": 11,
"d": 1,
"e": 4,
"f": 44,
"g": 23,
"h": 12,
"i": 22,
"j": 23,
"k": [{
"a": 1,
"b": 5,
"c": 9,
"d": 1
}, {
"a": 1,
"b": 5,
"c": 9,
"d": 1
}]
},
{
"a": 4,
"b": 8,
"c": 12,
"d": 1,
"e": 4,
"f": 44,
"g": 23,
"h": 12,
"i": 22,
"j": 23,
"k": [{
"a": 1,
"b": 5,
"c": 9,
"d": 1
}, {
"a": 1,
"b": 5,
"c": 9,
"d": 1
}]
}
]

var result = data.map(x=>({...x}));
console.log(result);

这是我安慰它时的数组:

[{
"a": 1,
"b": 5,
"c": 9,
"d": 1,
"e": 4,
"f": 44,
"g": 23,
"h": 12,
"i": 22,
"j": 23,
"k": []
},
{
"a": 2,
"b": 6,
"c": 10,
"d": 1,
"e": 4,
"f": 44,
"g": 23,
"h": 12,
"i": 22,
"j": 23,
"k": []
},
{
"a": 3,
"b": 7,
"c": 11,
"d": 1,
"e": 4,
"f": 44,
"g": 23,
"h": 12,
"i": 22,
"j": 23,
"k": []
},
{
"a": 4,
"b": 8,
"c": 12,
"d": 1,
"e": 4,
"f": 44,
"g": 23,
"h": 12,
"i": 22,
"j": 23,
"k": []
}
]

我希望在使用 .map() 函数时我的数组是完整的。

最佳答案

扩展只是浅拷贝 - 因此内部数组不会被复制。

对于简单的深层复制,请使用 JSON.stringifyJSON.parse

const data = [{"a":1,"b":5,"c":9,"d":1,"e":4,"f":44,"g":23,"h":12,"i":22,"j":23,"k":[{"a":1,"b":5,"c":9,"d":1},{"a":1,"b":5,"c":9,"d":1}]},{"a":2,"b":6,"c":10,"d":1,"e":4,"f":44,"g":23,"h":12,"i":22,"j":23,"k":[{"a":1,"b":5,"c":9,"d":1},{"a":1,"b":5,"c":9,"d":1}]},{"a":3,"b":7,"c":11,"d":1,"e":4,"f":44,"g":23,"h":12,"i":22,"j":23,"k":[{"a":1,"b":5,"c":9,"d":1},{"a":1,"b":5,"c":9,"d":1}]},{"a":4,"b":8,"c":12,"d":1,"e":4,"f":44,"g":23,"h":12,"i":22,"j":23,"k":[{"a":1,"b":5,"c":9,"d":1},{"a":1,"b":5,"c":9,"d":1}]}];

const res = data.map(x => JSON.parse(JSON.stringify(x)));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

还有支持不太好的 Object.fromEntries - 此功能目前处于草案阶段:

const data = [{"a":1,"b":5,"c":9,"d":1,"e":4,"f":44,"g":23,"h":12,"i":22,"j":23,"k":[{"a":1,"b":5,"c":9,"d":1},{"a":1,"b":5,"c":9,"d":1}]},{"a":2,"b":6,"c":10,"d":1,"e":4,"f":44,"g":23,"h":12,"i":22,"j":23,"k":[{"a":1,"b":5,"c":9,"d":1},{"a":1,"b":5,"c":9,"d":1}]},{"a":3,"b":7,"c":11,"d":1,"e":4,"f":44,"g":23,"h":12,"i":22,"j":23,"k":[{"a":1,"b":5,"c":9,"d":1},{"a":1,"b":5,"c":9,"d":1}]},{"a":4,"b":8,"c":12,"d":1,"e":4,"f":44,"g":23,"h":12,"i":22,"j":23,"k":[{"a":1,"b":5,"c":9,"d":1},{"a":1,"b":5,"c":9,"d":1}]}];

const res = data.map(x => Object.fromEntries(Object.entries(x)));
console.log(res);
.as-console-wrapper { max-height: 100% !important; top: auto; }

关于javascript - 如何在javascript中将属性与内部对象数组映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56577347/

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