3 回答

TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊
您可以嘗試以下方法
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貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊
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貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
所有的值都是用任意值定義的,以便提供一個(gè)有效的答案。
首先聲明和/或定義變量
然后流控制,如條件
if/else if/else
和/或三元let x = 0 > y ? z : a
indexLabel
由于具有兩個(gè)可能值之一,請(qǐng)避免重寫像定義兩次的對(duì)象那樣的代碼。如果您練習(xí)#1,那么重復(fù)的代碼膨脹就不會(huì)成為問題。
三元組的不同之處if/else if/else
在于它就像一個(gè)表達(dá)式:
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);
添加回答
舉報(bào)