我有一个嵌套的属性,我试图找到一种干净的方法来替换它而不使用一堆切片和拼接(当前我正在使用的外观是,我正在寻找一种带有或不带有lodash的更干净的解决方案)
仅更新reproducible_counter: 0
成为reproducible_counter: 1
id:0
和urls.id 85
data = [{
id: 0,
groupID: "65da6a",
urls: [{
id: 85,
searchedurl: "https://www.yahoo.com",
errorurl: "https://www.yahoo.com/error505",
count: 1,
reproducible: false,
reproducible_counter: 0
},
{
id: 84,
searchedurl: "https://www.gmail.com",
errorurl: "https://www.gmail.com/error404",
count: 1,
reproducible: false,
reproducible_counter: 0
}
]
},
{
id: 1
groupID: "d4127e",
urls: [{
id: 3,
searchedurl: "agwscc",
errorurl: "xyqa",
count: 1,
reproducible: false,
reproducible_counter: 0,
resolved: null
}]
}
];
答案 0 :(得分:1)
这是一个解决方案。
for(var index=0; index< data.length; index++){
data[index].urls.filter(u => u.id === 85)[0].reproducible_counter++;
}
答案 1 :(得分:0)
我再没有比这更简单的了。我们必须先匹配外部ID,然后再匹配内部ID。对不起,如果我把问题弄错了。并且假设id是唯一的。
data.some(outer => {
if (outer.id === 0) {
outer.urls.some(inner => {
if (inner.id === 85) {
inner.reproducible_counter = 1;
return true;
}
return false;
});
return true;
}
return false;
});
答案 2 :(得分:0)
“清理”是主观的,但是假设id是唯一的,一种可能的lodash解决方案是:
_.chain(data).find({id: 0}).get('urls').find({id: 85}).assign({reproducible_counter: 1}).value();
例如:
data = [
{
id: 0,
groupID: "65da6a",
urls: [
{
id: 85,
searchedurl: "https://www.yahoo.com",
errorurl: "https://www.yahoo.com/error505",
count: 1,
reproducible: false,
reproducible_counter: 0
},
{
id: 84,
searchedurl: "https://www.gmail.com",
errorurl: "https://www.gmail.com/error404",
count: 1,
reproducible: false,
reproducible_counter: 0
}
]
},
{
id: 0,
groupID: "d4127e",
urls: [
{
id: 85,
searchedurl: "agwscc",
errorurl: "xyqa",
count: 1,
reproducible: false,
reproducible_counter: 0,
resolved: null
}
]
}
];
// Goal: updating only reproducible_counter: 0 to become reproducible_counter: 1 within id:0 and urls.id 85
_.chain(data).find({id: 0}).get('urls').find({id: 85}).assign({reproducible_counter: 1}).value();
document.getElementById('result').innerText = JSON.stringify(data, null, 4);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
<pre id="result"></pre>
答案 3 :(得分:0)
使用ES6 chain
:
var data = [{ id: 0, groupID: "65da6a", urls: [{ id: 85, searchedurl: "https://www.yahoo.com", errorurl: "https://www.yahoo.com/error505", count: 1, reproducible: false, reproducible_counter: 0 }, { id: 84, searchedurl: "https://www.gmail.com", errorurl: "https://www.gmail.com/error404", count: 1, reproducible: false, reproducible_counter: 0 } ] }, { id: 1, groupID: "d4127e", urls: [{ id: 3, searchedurl: "agwscc", errorurl: "xyqa", count: 1, reproducible: false, reproducible_counter: 0, resolved: null }] } ];
var result = data
.find(({id}) => id === 0)
.urls
.find(({id}) => id === 85)
.reproducible_counter = 1
console.log(data[0].urls[0].reproducible_counter)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
使用lodash _.chain
:
var data = [{ id: 0, groupID: "65da6a", urls: [{ id: 85, searchedurl: "https://www.yahoo.com", errorurl: "https://www.yahoo.com/error505", count: 1, reproducible: false, reproducible_counter: 0 }, { id: 84, searchedurl: "https://www.gmail.com", errorurl: "https://www.gmail.com/error404", count: 1, reproducible: false, reproducible_counter: 0 } ] }, { id: 1, groupID: "d4127e", urls: [{ id: 3, searchedurl: "agwscc", errorurl: "xyqa", count: 1, reproducible: false, reproducible_counter: 0, resolved: null }] } ];
_.chain(data)
.find({id: 0})
.get('urls')
.find({id: 85})
.set('reproducible_counter', 1)
.value()
console.log(_.get(data, '0.urls.0.reproducible_counter'))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>