我使用vuecli 4.3.1vue: 2.6.11。
我在json中有一个数组,我想根据另一个字段的值在其中添加值。
"rules":[{"id":1,"name":"new rule","patient":[],"code":[1],"to_code":[1]}]
如果代码数组包含代码值的索引==50,那么to_code就包含代码值的索引100,否则to_code在这个规则代码中为空。
{"code":[{"id":1,"name":"New Item","values":[{"id":1,"code":"50"}]}]
to_code:
"to_code":[{"id":1,"name":"To code 100","values":100}
JSON中的数据在此函数中收集。
saveData() {
let params = {
data: {
code: this.codeList,
to_code: this.toCodeList
rules: this.pacList
}
};
在vuex.Store(Local)中保存Vue的状态。
toCodeList: state => state.to_codes,
codeList: state => state.codes,
保存Vue Store(Local)中的这个函数。
saveRule() {
if (this.ruleItem.id > 0) {
console.log(this.ruleItem);
} else {
let newId = 1;
if (this.pacList.length > 0) {
newId =
Math.max.apply(
Math,
this.pacList.map(function(o) {
return o.id;
})
) + 1;
}
this.ruleItem.id = newId;
this.pacList.push(this.ruleItem);
}
创建新规则函数。
createRule() {
this.ruleItem = { ...this.emptyRule };
空规则。
emptyRule: {
id: 0,
name: "new rule",
code: [],
to_code:[]
}
我试图在saveRule函数中做什么。
var parsedobj = JSON.parse(JSON.stringify(this.codeList));
parsedobj.forEach(arrayItem => {
var x = arrayItem.values;
for (let item = 0; item < x.length; item++) {
const element = x[item].id;
const codenumber = x[item].code;
if (codenumber == 50 || codenumber == 200) {
let correctID = x[item].id;
var parsepaclist = JSON.parse(JSON.stringify(this.pacList));
parsepaclist.forEach(pacitem => {
for (let index = 0; index < parsepaclist.length; index++) {
const IDofpaclist = parsepaclist[index].id;
const Itemofpaclist = parsepaclist[index];
var numberofDestPort = pacitem.code;
if (correctID == Itemofpaclist.code) {
if (codenumber == 50) {
let newtocode = {
id: 1,
name: "To code 100",
values: 100,
active: false
};
this.tocodelist.push(newtocode);
Itemofpaclist.to_code.push(1);
if (Itemofpaclist.id == this.ruleItem.id) {
this.ruleItem.to_code = Itemofpaclist.to_code;
}
} else if (codenumber == 200) {
let newtocode = {
id: 2,
name: "To Code 300",
values: 300,
active: false
};
this.tocodelist.push(newtocode);
Itemofpaclist.to_code.push(2);
if (Itemofpaclist.id == this.ruleItem.id) {
this.ruleItem.to_code = Itemofpaclist.to_code;
}
}
}
}
});
}
}
});
问题是当我在Json中添加代码50toCode对象时,不包含代码值100的索引,或者代码200toCode对象在Json中不包含代码值300的索引。
new Vue({
el: '#content',
data: {
code: {},
to_code: {},
rule: {}
},
created: function () {
this.code = Object.assign({"code":[{"id":1,"name":"New Item","values":[{"id":1,"code":"50"}]}]})
this.to_code = Object.assign({"to_code":[{"id":1,"name":"To code 100","values":100}]})
// this.rule =
for (var i = this.code.code.length - 1; i >= 0; i--) {
var code = this.code.code[i]['values']
for (var k = code.length - 1; k >= 0; k--) {
if (code[k]['code'] == 50) {
for (var j = this.to_code.to_code.length - 1; j >= 0; j--) {
var to_code = this.to_code.to_code[j]['values']
if (to_code == 100) {
this.rule = Object.assign({"rules":[{"id":1,"name":"new rule","patient":[],"code": this.code.code[i],"to_code":this.to_code.to_code[j]}]})
}
}
} else {
this.rule = Object.assign({"rules":[{"id":1,"name":"new rule","patient":[],"code": this.code.code[i],"to_code":[]}]})
}
};
};
console.log(JSON.stringify(this.rule))
}
})
你可以在这个编码结构中设置你的需求和代码。 谢谢。