3 回答
TA貢獻1806條經驗 獲得超8個贊
您可以嘗試以下方法
dps_cote.push({
x: i,
label: cote[i].x,
y: cote[i].y,
color: 'green',
indexLabelFontColor : "red",
indexLabelFontWeight: "bold",
indexLabel: marge[i].cas_v?marge[i].cas_v.toString():""
});
TA貢獻1804條經驗 獲得超2個贊
let object = {
x: i,
label: cote[i].x,
y: cote[i].y,
color: 'green',
indexLabelFontColor : "red",
indexLabelFontWeight: "bold",
}
if (marge[i].cas_v) {
object.indexLabel = marge[i].cas_v.toString();
dps_cote.push(object);
} else {
dps_cote.push(object);
}
TA貢獻1831條經驗 獲得超4個贊
所有的值都是用任意值定義的,以便提供一個有效的答案。
首先聲明和/或定義變量
然后流控制,如條件
if/else if/else和/或三元let x = 0 > y ? z : aindexLabel由于具有兩個可能值之一,請避免重寫像定義兩次的對象那樣的代碼。如果您練習#1,那么重復的代碼膨脹就不會成為問題。
三元組的不同之處if/else if/else在于它就像一個表達式:
let iL = marge[i].cas_v ? marge[i].cas_v.toString() : "";
/*
if `marge[i].cas_v` exists then `iL` is `marge[i].cas_v.toString()`
otherwise it is `""`
*/
obj.indexLabel = iL;
// whatever `iL` ends up to be -- its assigned to `obj.indexLabel`
演示
/*
if i = 0 then indexLabel: "11"
if i = 1 then indexLabel: "121"
if i = 2 then indexLabel: "14641"
*/
let i = 0;
let cote = [{x: 0, y: 0}, {x: 1, y: 0}, {x: 0, y: 1}];
let marge = [{cas_v: 11}, {cas_v: 121}, {cas_v: 14641}];
let dps_cote = [];
let obj = {
x: i,
label: cote[i].x,
y: cote[i].y,
color: 'green',
indexLabelFontColor: "red",
indexLabelFontWeight: "bold",
indexLabel: ""
};
let iL = marge[i].cas_v ? marge[i].cas_v.toString() : "";
obj.indexLabel = iL;
dps_cote.push(obj);
console.log(dps_cote);
添加回答
舉報
