请耐心等待,因为这是一个我正在努力解决的问题所以我可能不会很好地解释它!
我有一个值数组和两个字符串: e.g。
["5", "6"]
"DEPOT_NAME",
"DEPOT_ID"
我也有一个对象数组(在这个例子中只有一个但可以是多个):
[
{
columns: [
{name: "DEPOT_ID"},
{name: "DEPOT_NAME"},
{name; "REGION_ID"},
{name: "CREATED_DATE"},
{name: "MODIFIED_DATE"}
],
id: 1,
rows: [
{data: {DEPOT_ID: "0", DEPOT_NAME: "London", REGION_ID: "0", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}},
{data: {DEPOT_ID: "1", DEPOT_NAME: "Abbey Road", REGION_ID: "1", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}},
{data: {DEPOT_ID: "3", DEPOT_NAME: "Romford", REGION_ID: "3", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}},
{data: {DEPOT_ID: "5", DEPOT_NAME: "Bristol", REGION_ID: "5", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}},
{data: {DEPOT_ID: "6", DEPOT_NAME: "Croydon", REGION_ID: "6", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}}
],
title: "Depots"
}
]
使用我拥有的值,我需要获取与DEPOT_NAME
值对应的DEPOT_ID
键的值。上面的数组可以用来找到我假设的正确的仓库ID,但是我怎样才能获得仓库名称的值?
我希望我解释说好的,如果你需要更多澄清,我会尽力让它更清楚。
非常感谢任何帮助。
感谢您的时间。
答案 0 :(得分:1)
此示例生成一个包含元素“Bristol”和“Croydon”的数组(基于具有[5,6]的id数组):
// your example object
var yourObj = [
{
columns: [
{name: "DEPOT_ID"},
{name: "DEPOT_NAME"},
{name: "REGION_ID"},
{name: "CREATED_DATE"},
{name: "MODIFIED_DATE"}
],
id: 1,
rows: [
{data: {DEPOT_ID: "0", DEPOT_NAME: "London", REGION_ID: "0", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}},
{data: {DEPOT_ID: "1", DEPOT_NAME: "Abbey Road", REGION_ID: "1", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}},
{data: {DEPOT_ID: "3", DEPOT_NAME: "Romford", REGION_ID: "3", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}},
{data: {DEPOT_ID: "5", DEPOT_NAME: "Bristol", REGION_ID: "5", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}},
{data: {DEPOT_ID: "6", DEPOT_NAME: "Croydon", REGION_ID: "6", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03"}}
],
title: "Depots"
}
];
// example code to fetch the depot names based on ids
var ids = [5, 6];
var depotNames = [];
for(var i = 0; i<yourObj.length; i++){
var rows = yourObj[i].rows;
for(var j =0; j < rows.length; j++){
for(id in ids){
if(rows[j].data.DEPOT_ID == ids[id]){
depotNames.push(rows[j].data.DEPOT_NAME);
}
}
}
}
答案 1 :(得分:1)
您可以迭代对象并过滤所需的部分并仅获取目标值。
function getValues(key, values, target) {
return data[0].rows.filter(function (a) {
return values.indexOf(a.data[key]) !== -1;
}).map(function (a) { return a.data[target]; });
}
var data = [{ columns: [{ name: "DEPOT_ID" }, { name: "DEPOT_NAME" }, { name: "REGION_ID" }, { name: "CREATED_DATE" }, { name: "MODIFIED_DATE" }], id: 1, rows: [{ data: { DEPOT_ID: "0", DEPOT_NAME: "London", REGION_ID: "0", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }, { data: { DEPOT_ID: "1", DEPOT_NAME: "Abbey Road", REGION_ID: "1", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }, { data: { DEPOT_ID: "3", DEPOT_NAME: "Romford", REGION_ID: "3", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }, { data: { DEPOT_ID: "5", DEPOT_NAME: "Bristol", REGION_ID: "5", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }, { data: { DEPOT_ID: "6", DEPOT_NAME: "Croydon", REGION_ID: "6", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }], title: "Depots" }];
console.log(getValues("DEPOT_ID", ["5", "6"], "DEPOT_NAME"));
ES6
function getValues(key, values, target) {
return data[0].rows.
filter(a => values.indexOf(a.data[key]) !== -1).
map(a => a.data[target]);
}
var data = [{ columns: [{ name: "DEPOT_ID" }, { name: "DEPOT_NAME" }, { name: "REGION_ID" }, { name: "CREATED_DATE" }, { name: "MODIFIED_DATE" }], id: 1, rows: [{ data: { DEPOT_ID: "0", DEPOT_NAME: "London", REGION_ID: "0", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }, { data: { DEPOT_ID: "1", DEPOT_NAME: "Abbey Road", REGION_ID: "1", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }, { data: { DEPOT_ID: "3", DEPOT_NAME: "Romford", REGION_ID: "3", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }, { data: { DEPOT_ID: "5", DEPOT_NAME: "Bristol", REGION_ID: "5", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }, { data: { DEPOT_ID: "6", DEPOT_NAME: "Croydon", REGION_ID: "6", CREATED_DATE: "2016-05-03", MODIFIED_DATE: "2016-05-03" } }], title: "Depots" }];
console.log(getValues("DEPOT_ID", ["5", "6"], "DEPOT_NAME"));
答案 2 :(得分:0)
function fetchData(cols, ids, mainArray) {
var data = [];
for(var i=0;i < mainArray.length; i++) {
for(var c =0;c<mainArray[i].rows.length;c++) {
for(var id in ids) {
if(mainArray[i].rows[c].data.DEPOT_ID == ids[id]) {
var temp = {};
for(col in cols) {
temp[cols[col]] = mainArray[i].rows[c].data[cols[col]];
}
data.push(temp);
}
}
}
}
return data;
}
用法:fetchData([&#34; DEPOT_NAME&#34;,&#34; DEPOT_ID&#34;],[&#34; 5&#34;,&#34; 6&#34;],YourArray);