简体   繁体   English

操作数组和对象

[英]Manipulating arrays and objects

Bear with me as this is a problem that I am struggling with so I might not explain it very well! 请忍受,因为这是我一直在努力解决的问题,因此我可能无法很好地解释它!

I have an array of values and two strings: eg 我有一个值数组和两个字符串:例如

["5", "6"]
"DEPOT_NAME",
"DEPOT_ID"

I also have an array of objects (only one in this example but can be multiple): 我还有一个对象数组(在此示例中只有一个,但可以多个):

[
 {
  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"
 }
]

Using the values that I have I need to get the value for the DEPOT_NAME key that corresponds to the correct DEPOT_ID value. 使用我拥有的值,我需要获取与正确的DEPOT_ID值相对应的DEPOT_NAME键的值。 The array above can be used to find the correct depot ids I assume but then how would I go about get the value for the depot name? 上面的数组可用于查找我假设的正确的仓库ID,但是我将如何获取仓库名称的值?

I hope I explained that ok, if you need more clarification I will try my best to make it clearer. 希望我能解释一下,如果您需要更多说明,我会尽力使它更清楚。

Any help with this would be much appreciated. 任何帮助,将不胜感激。

Thanks for your time. 谢谢你的时间。

This example produces an array with the elements "Bristol" and "Croydon" (based on an array of ids with [5, 6]): 本示例生成一个包含元素“ 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);
       }
     }

   }
}

You could iterate the object and filter the wanted parts and get only the target value. 您可以迭代对象并过滤所需部件,而仅获得目标值。

 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 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")); 

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;
}

Usage: fetchData(["DEPOT_NAME", "DEPOT_ID"], ["5", "6"], YourArray); 用法:fetchData([[“ DEPOT_NAME”,“ DEPOT_ID”],[“ 5”,“ 6”],YourArray);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM