这是我的数组。里面有多个对象,其中一些具有
我想检查数组对象是否存在未定义的值,如果存在任何未定义的值,则应附加下一个对象。
例如,在第一个元素
我知道
最终数组必须是:
undefined
值。var array = [
{ 0: undefined, 1: 32, 2: "four" },
{ 0: undefined, 1: undefined, 2: "three" },
{ 0: undefined, 1: 24, 2: "two" },
{ 0: 14, 1: 24, 2: "five" },
{ 0: 10, 1: 21, 2: "one" },
]
我想检查数组对象是否存在未定义的值,如果存在任何未定义的值,则应附加下一个对象。
例如,在第一个元素
{0: undefined, 1: 32, 2: "four"}
中,我有一个0:undefined
,第二个{0: undefined, 1: undefined, 2: "three"}
元素应替换它,并且应该形成新的对象{0: {0: undefined, 1: undefined, 2: "three"}, 1: 32, 2: "four"}
。我知道
Array.forEach
可以做到这一点。 如果是类似的数组,我可以使用Array.splice()
和Array.lastIndexOf()
。 首先从右侧或最后一个未定义值开始搜索,然后在其中附加下一个数组,然后再次执行此操作。最终数组必须是:
{
0: {
0: {
0: 10,
1: 21,
2: "one"
},
1: {
0: {
0: 14,
1: 24,
2: "five"
},
1: 24,
2: "two"
},
2: "three"
},
1: 32,
2: "four"
}
if (Object.values(element).some(el => el === undefined))
来检查循环中当前元素是否存在任何undefined
值。希望这能帮助你入门。 - Barmar