Javascript 若数组中不存在索引,则在索引位置创建元素
我有一个包含id和名称的数组对象Javascript 若数组中不存在索引,则在索引位置创建元素,javascript,arrays,Javascript,Arrays,我有一个包含id和名称的数组对象 const stages = [{ id: 1, name: '' }, { id: 2, name: '' }, { id: 3, name: '' }, { id: 4, name: '' }, { id: 5, name: '' }, { id: 6, name: '' }, { id: 7, name: '' }, { id: 8, name: '' }]; 此外,我还有一个保存数字的数组
const stages = [{
id: 1,
name: ''
}, {
id: 2,
name: ''
}, {
id: 3,
name: ''
}, {
id: 4,
name: ''
}, {
id: 5,
name: ''
}, {
id: 6,
name: ''
}, {
id: 7,
name: ''
}, {
id: 8,
name: ''
}];
此外,我还有一个保存数字的数组
const indexPositions = [0, 1, 2, 2, 2, 3, 2, 0];
我想创建第三个包含数组的数组。距离中的每个数字表示数组中当前数组的索引
如果当前数组还不存在,我想先创建它。显然,我必须创建新的数组,直到我到达这个索引位置
例如:
我的数组在开始时是空的。第一个索引位置是0,因此我必须为此创建一个新数组。下一个索引位置是3,所以我必须创建更多的数组,直到我有4个数组
我想做的就是把舞台推到正确的水平位置。这个例子的结果是
const levels = [
[stage1, stage8],
[stage2],
[stage3, stage4, stage5, stage7],
[stage6]
];
目前我的代码如下所示
$(document).ready(() => {
const levels = []; // the array containing the arrays
stages.forEach((stage, stageIndex) => {
const indexPosition = indexPositions[stageIndex];
const positionDifference = indexPosition - levels.length;
if (positionDifference > 0) {
for (let i = 0; i < positionDifference; i++) { // fill up with empty arrays
levels.push([]);
}
}
levels[indexPosition].push(stage);
});
});
$(文档).ready(()=>{
const levels=[];//包含数组的数组
stages.forEach((stage,stageIndex)=>{
常数indexPosition=indexPositions[stageIndex];
const position difference=索引位置-levels.length;
如果(位置差>0){
对于(设i=0;i
我得到了这个错误uncaughttypeerror:无法读取未定义的属性“push”
,这是因为indexPosition超出了范围。如果positionDifference
为0,则不会创建任何数组,但在开始时数组为空
我尝试将levels.length设置为-1(如果为0),但如果差值为1,我仍然会得到错误。我在位置0处创建了一个数组,并希望访问位置1
如果空数组不存在,如何创建它?虽然我不完全理解您想要做什么,但检查数组元素是否存在很简单,一种方法是将其强制为布尔值:
const thing=[];
函数加法器(何处,何处){
如果(!thing[where])/在我不完全理解您想要做什么的时候,检查数组元素的存在性很简单,一种方法是将其强制为布尔值:
const thing=[];
函数加法器(何处,何处){
如果(!thing[where])/您可以使用单个循环,如果不存在索引,则为索引添加一个数组。然后推送所需的值
var stages=[{id:1,名称:''},{id:2,名称:''},{id:3,名称:''},{id:4,名称:''},{id:5,名称:''},{id:6,名称:'''},{id:7,名称:'''},{id:8,名称:''''},],
indexPositions=[0,1,2,2,2,3,2,0],
结果=阶段。减少((r,o,i)=>{
var指数=指数扩张[i];
r[index]=r[index]| |[];//取falsy值的默认值
r[index].push('stage'+o.id);//而不是字符串take对象
返回r;
}, []);
console.log(结果);
.as console wrapper{max height:100%!important;top:0;}
如果不存在索引,可以使用单个循环并为索引添加数组。然后推送所需的值
var stages=[{id:1,名称:''},{id:2,名称:''},{id:3,名称:''},{id:4,名称:''},{id:5,名称:''},{id:6,名称:'''},{id:7,名称:'''},{id:8,名称:''''},],
indexPositions=[0,1,2,2,2,3,2,0],
结果=阶段。减少((r,o,i)=>{
var指数=指数扩张[i];
r[index]=r[index]| |[];//取falsy值的默认值
r[index].push('stage'+o.id);//而不是字符串take对象
返回r;
}, []);
console.log(结果);
.as console wrapper{max height:100%!important;top:0;}
您实际上非常接近!您的代码中有一个非常小的问题
$(document).ready(() => {
const levels = []; // the array containing the arrays
stages.forEach((stage, stageIndex) => {
const indexPosition = indexPositions[stageIndex];
const positionDifference = indexPosition - levels.length + 1; //YOU DID NOT ADD 1 HERE
if (positionDifference > 0) {
for (let i = 0; i < positionDifference; i++) { // fill up with empty arrays
levels.push([]);
}
}
levels[indexPosition].push(stage);
});
});
$(文档).ready(()=>{
const levels=[];//包含数组的数组
stages.forEach((stage,stageIndex)=>{
常数indexPosition=indexPositions[stageIndex];
const positionDifference=indexPosition-levels.length+1;//您没有在此处添加1
如果(位置差>0){
对于(设i=0;i
在计算位置差时,没有添加1,当indexPosition等于0且for循环未运行且未推送新数组时,会导致问题。只添加一个解决了问题:-)您实际上非常接近!代码中存在一个非常小的问题
$(document).ready(() => {
const levels = []; // the array containing the arrays
stages.forEach((stage, stageIndex) => {
const indexPosition = indexPositions[stageIndex];
const positionDifference = indexPosition - levels.length + 1; //YOU DID NOT ADD 1 HERE
if (positionDifference > 0) {
for (let i = 0; i < positionDifference; i++) { // fill up with empty arrays
levels.push([]);
}
}
levels[indexPosition].push(stage);
});
});
$(文档).ready(()=>{
const levels=[];//包含数组的数组
stages.forEach((stage,stageIndex)=>{
常数indexPosition=indexPositions[stageIndex];
const positionDifference=indexPosition-levels.length+1;//您没有在此处添加1
如果(位置差>0){
对于(设i=0;i
在计算位置差时,您没有添加1,当indexPosition等于0,并且for循环没有运行,也没有推送新数组时,会导致问题。只添加一个解决了问题:-)哦,这看起来很容易,这看起来很容易