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

I'm trying to make an if statements that sorts different types of values in my array of objects

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

带对象的数组。

var stoneData = [];
stoneData.push({type:"lineTo", point:"test", axis:{x:0, y:0}} + ' = test data 1');
stoneData.push( ' ' + {type:"moveTo", point:"test", axis:{x:0, y:0}} + ' = test data 2');
stoneData.push(' ' + {type:"quadraticCurveTo", point:"test", 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 + '].point = ' + stoneData[i].point + ', stoneData['+i+'].axis.curveX = ' + stoneData[i].axis.curveX + ', stoneData['+i+'].axis.curveY = ' + stoneData[i].axis.curveY + ', stoneData['+i+'].axis.x = ' + stoneData[i].axis.x + ', 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 + '].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 + '].point = ' + stoneData[i].point + ', stoneData['+i+'].axis.curveX = ' + stoneData[i].axis.curveX + ', stoneData['+i+'].axis.curveY = ' + stoneData[i].axis.curveY + ', stoneData['+i+'].axis.x = ' + stoneData[i].axis.x + ', 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", point:"test", axis:{x:0, y:0}} + ' = test data 1');
stoneData.push( ' ' + {type:"moveTo", point:"test", axis:{x:0, y:0}} + ' = test data 2');
stoneData.push(' ' + {type:"quadraticCurveTo", point:"test", axis:{curveX:0, curveY:0, x:0, 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 + '].point = ' + stoneData[i].point + ', stoneData['+i+'].axis.x = ' + stoneData[i].axis.x + ', stoneData['+i+'].axis.y = ' + stoneData[i].axis.y);
    break;
  case "quadraticCurveTo":
    console.log('stoneData[' + i + '].type = ' + stoneData[i].type + ', stoneData[' + i + '].point = ' + stoneData[i].point + ', stoneData['+i+'].axis.curveX = ' + stoneData[i].axis.curveX + ', stoneData['+i+'].axis.curveY = ' + stoneData[i].axis.curveY + ', stoneData['+i+'].axis.x = ' + stoneData[i].axis.x + ', 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", point: 112, axis: { x: 123, y: 123 } },
    { type: "moveTo", point: 112, axis: { x: 123, y: 123 } },
    { type: "quadraticCurveTo", point: 112, axis: { x: 123, y: 123 } },
    { type: "xxy", point: 112, axis: { x: 123, 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");
    }
    
}