1

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: e.g.

["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. 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?

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.

3
  • Do you want to get Depot_Name using Depot_Id right? Commented Sep 9, 2016 at 10:14
  • Can you explain us with example by providing Input and Final output you need for a scenario Commented Sep 9, 2016 at 10:16
  • So basically I want to get a value of ["Bristol", "Croydon"] by using the array ["5", "6"] to find the correct depot ids and then get the value of the depot name. Make more sense? Commented Sep 9, 2016 at 10:19

3 Answers 3

1

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

nice, clean solution
Thank you very much for this!
1

This example produces an array with the elements "Bristol" and "Croydon" (based on an array of ids with [5, 6]):

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

   }
}

Comments

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

Usage: fetchData(["DEPOT_NAME", "DEPOT_ID"], ["5", "6"], YourArray);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.