2

I have array of objects , and would like to fetch some attributes based on another one

assume

var lngs = [{id : 3 , label : 'a'} , {id : 2 , label : 'b' , {id : 4 , label : 'c'}];

now I need to get array of labels based on ids

for example

getAttrs([4,3]);

function getAttrs(ids , arr)
{
   // as long as ids = [4,3] , it should return
   ["c","a"]
}

I tried this algorithm , but it did not work as expected

 langsFromIds(ids)
 {
   var rslt = [];
   var lngs = this.state.langs;
   for (var i = 0 ; i < lngs.length ; i++)
   {
     if (lngs[i].id == ids[i])
     {
       rslt.push(lngs[i].label);
     }
   }

   return rslt;
 }

because it runs in this way (for example)

first iteration (i==0) => lngs[i].id == 3 && ids[i] == 4 (will not match)

second iteration (i==1) => lngs[i].id == 2 && ids[i] == 3 (will not match)

and so on ..

so it will return empty array

2
  • 1
    You need to loop inside again and not match index to index. Commented Jul 10, 2019 at 14:02
  • 1
    Your lngs object is syntactically invalid. I have posted my answer assuming var lngs = [{id : 3 , label : 'a'} , {id : 2 , label : 'b'} , {id : 4 , label : 'c'}]; Commented Jul 10, 2019 at 14:03

4 Answers 4

1

First filter down to the corrects ids, then extract the desired labels:

var lngs = [{id : 3 , label : 'a'} , {id : 2 , label : 'b'} , {id : 4 , label : 'c'}];

function langsFromIds(ids, langs)
{
  return langs.filter(lng => ids.indexOf(lng.id) > -1).map(lng => lng.label);
}

console.log(langsFromIds([4, 3], lngs));

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

1 Comment

It works well I think , thanks I'll accept your answer
1

You could use the filter and find to get a list of individual lng objects...

var lngs = [{id:3,label:'a'},{id:2,label:'b'},{id:4,label:'c'}];

function lngsFromIds(ids) {
  return lngs.filter(x => ids.find(y => y == x.id));
}
console.log(lngsFromIds([3,4]));

And then use map to get the label as well...

var lngs = [{id:3,label:'a'},{id:2,label:'b'},{id:4,label:'c'}];

function labelsFromIds(ids) {
  return lngs.filter(x => ids.find(y => y == x.id)).map(z => z.label);
}
console.log(labelsFromIds([3,4]));

Comments

0

Here is my proposition :

var lngs = [{id : 3 , label : 'a'} , {id : 2 , label : 'b' }, {id : 4 , label : 'c'}];

var npq = getAttrs([4,3]);
console.log(npq);

function getAttrs(ids)
{
   var rslt = [];
   //var lngs = this.state.langs;
    for (var k = 0; k < ids.length; k++)
    {
        for (var i = 0 ; i < lngs.length ; i++)
        {
          if (lngs[i].id == ids[k])
          {
             rslt.push(lngs[i].label);
          }
        }
    }
   return rslt;
}

Comments

0
  • To answer your question, you are matching index to index and hence you won't get the desired result as the id could be found anywhere in ids.

  • A quick solution is to iterate inside again on ids and if a match is found, you add them in the result.

var lngs = [{id:3,label:'a'},{id:2,label:'b'},{id:4,label:'c'}];

function langsFromIds(ids)
 {
   var rslt = [];
   for (var i = 0 ; i < lngs.length ; i++)
   {
     for(var j=0; j < ids.length;j++)
     {
      	if (lngs[i].id == ids[j])
     	{
       		rslt.push(lngs[i].label);
     	}	 
     }
   }

   return rslt;
 }

console.log(langsFromIds([3,4]));

  • Although the above solution works, it isn't scalable, meaning above solution would take a lot of time to return a result if size of lngs goes huge along with ids. In other words, time complexity is O(n * m) where n is the size of lngs and m is the size of ids.

  • So, if you are aiming for an efficient approach, you could make a set of ids and check for a match in lngs.

var lngs = [{id:3,label:'a'},{id:2,label:'b'},{id:4,label:'c'}];

function lngsFromIds(ids) {
  var set = new Set(ids);
  return lngs.filter(x => set.has(x.id)).map(x => x.label);
}
console.log(lngsFromIds([3,4]));

  • Above approach can decide whether the current object's id is present in ids or not in on an average O(1) time, hence making the function faster. Time complexity would be O(n + m) time with an additional space trade off of O(m) where n and m are same as mentioned before.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.