我正在尝试创建一个 if 语句来对我的对象数组中的不同类型的值进行排序

问题描述

我想要一个代码,它为我提供对象属性值的值,然后采用具有该值的 if 语句,以便获得正确的 console.log。

带对象的数组。

var stoneData = [];
stoneData.push({type:"lineto",point:"test",axis:{x:0,y:0}} + ' = test data 1');
stoneData.push( ' ' + {type:"moveto",y:0}} + ' = test data 2');
stoneData.push(' ' + {type:"quadraticCurveto",axis:{curveX:0,curveY:0,x:0,y:0}} + '=  test data 3');

If 语句

    var i = 0;
if(stoneData[i].type == "moveto" || stoneData[i].type == "lineto" ) {
console.log('stoneData[' + i + '].type = ' + stoneData[i].type + ',stoneData[' + i + '].point = ' + stoneData[i].point + ',stoneData['+i+'].axis.x = ' + stoneData[i].axis.x + ',stoneData['+i+'].axis.y = ' + stoneData[i].axis.y);
} else if(stoneData[i].type == "quadraticCurveto") {
  console.log('stoneData[' + i + '].type = ' + stoneData[i].type + ',stoneData['+i+'].axis.curveX = ' + stoneData[i].axis.curveX + ',stoneData['+i+'].axis.curveY = ' + stoneData[i].axis.curveY + ',stoneData['+i+'].axis.y = ' + stoneData[i].axis.y);
} else {
  return console.log("something went wrong");
}
//next console.log(stoneData[i]);
 i += 1;
 if(stoneData[i].type == "moveto" || stoneData[i].type == "lineto" ) {
 console.log('stoneData[' + i + '].type = ' + stoneData[i].type + ',stoneData['+i+'].axis.y = ' + stoneData[i].axis.y);
 } else if(stoneData[i].type == "quadraticCurveto") {
   console.log('stoneData[' + i + '].type = ' + stoneData[i].type + ',stoneData['+i+'].axis.y = ' + stoneData[i].axis.y);
 } else {
   return console.log("something went wrong");
 }

我也尝试过其他不同的方法

我找不到正确的查找方法 (stoneData[index].type == value)。

我做编程还不到一年,所以我还不太擅长预先回答。

这是新代码

var stoneData = [];
stoneData.push({type:"lineto",y:0}} + '=  test data 3');

    function consoleTheData() {
//log the data
console.log('stoneData = ' + stoneData);
console.log('stoneData.length in index = ' + (stoneData.length - 1));
for (let i = 0; i < stoneData.length; i++) {
switch (stoneData[i].type) {
  case "moveto":
  case "lineto":
    console.log('stoneData[' + i + '].type = ' + stoneData[i].type + ',stoneData['+i+'].axis.y = ' + stoneData[i].axis.y);
    break;
  case "quadraticCurveto":
    console.log('stoneData[' + i + '].type = ' + stoneData[i].type + ',stoneData['+i+'].axis.y = ' + stoneData[i].axis.y);
    break;
  default:
    console.log("something went wrong");
}
}
}
consoleTheData();

解决方法

尝试这样的事情

const stoneData = [
    { type: "moveTo",point: 112,axis: { x: 123,y: 123 } },{ type: "lineTo",{ type: "moveTo",{ type: "quadraticCurveTo",{ type: "xxy",y: 123 } }
];


stoneData.map((stone) =>        
        switch (stone.type) {
            case "moveTo":
            case "lineTo":
                console.log("case 1 - lineTo or moveTo" );
                break;
            case "quadraticCurveTo":
                console.log("case 2 quadraticCurveTo ");
                break;
            default:
                console.log("something went wrong");
        }
}); 

输出

case 1 - lineTo or moveTo
case 1 - lineTo or moveTo
case 1 - lineTo or moveTo
case 2 quadraticCurveTo 
something went wrong

与for循环相同

for (let i = 0; i < stoneData.length; i ++) {

    switch (stoneData[i].type) {
        case "moveTo":
        case "lineTo":
            console.log("case 1 - lineTo or moveTo");
            break;
        case "quadraticCurveTo":
            console.log("case 2 quadraticCurveTo ");
            break;
        default:
            console.log("something went wrong");
    }
    
}