gpt4 book ai didi

Javascript 对象映射(展平)

转载 作者:行者123 更新时间:2023-11-28 18:42:11 30 4
gpt4 key购买 nike

您好,我有一个使用 JSON.Stringify 输出到此的对象

{"0":["test1","ttttt","","","","","","","",""],"1":["test2","ghjgjhgjh","","","","","","","",""]}

我想要这样的输出。

[["test1","ttttt","","","","","","","",""],["test2","ghjgjhgjh","","","","","","","",""]]

我尝试使用 .map 删除“0”和“1”

var itemjson = $.map(cleanedGridData, function (n) {
return n;
});

然而,这给出了(如下)的输出,该输出已经变平了。

["test1", "ttttt", "", "", "", "", "", "", "", "", "test2", "ghjgjhgjh", "", "", "", "", "", "", "", ""]

最佳答案

您可以使用它来提取值:

var res = {"0":["test1","ttttt","","","","","","","",""],"1":["test2","ghjgjhgjh","","","","","","","",""]}
Object.keys(res).map(function(key) {
return res[key];
});

Object.keys 将列出初始对象中的所有键。然后,您可以使用 map 迭代这些键并在该函数中提取值。

关于Javascript 对象映射(展平),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35953611/

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