【发布时间】:2020-08-24 09:05:53
【问题描述】:
我正在使用 vue/cli 4.3.1 Vue: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 中的状态:
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;
}
}
}
}
});
}
}
});
问题是当我添加代码 50 Json 中的 toCode 对象不包含代码值 100 的索引 或在 Json 中的代码 200 toCode 对象中不包含代码值 300 的索引
【问题讨论】:
-
您提供了很多信息,但仍然未能解释您的问题究竟是什么。你想达到什么目标?你有什么尝试但没有奏效?
-
我按照我的尝试编辑了这个问题
-
我多次阅读您的询问,但我不明白您的意思.. 您能解释一下这个“问题是当我在 Json 中添加代码 50 toCode 对象时不包含 to 的索引Json 中的代码值 100 或代码 200 中的 toCode 对象不包含代码值 300" 的索引?
-
我还看到你定义了
codenumber并使用了它if (codenumber == 50 || codenumber == 200) {然后你在子条件if (portnumber == 50) {和} else if (portnumber == 200) {中使用了portnumber这可能是你的代码有问题!跨度> -
我的意思是当“code”对象在代码对象中包含值50的索引时,我希望Json中的to_code对象包含to_code对象中值100的索引数
标签: javascript json vue.js vuex