2

This is my static values which is having id with names:

var staticValues = [
  { "id": "1", "name": "PEKKA" },
  { "id": "2", "name": "Golem" },
  { "id": "3", "name": "Vigilane" },
  { "id": "4", "name": "SpiderMan" },
  { "id": "5", "name": "Archer" },
  { "id": "6", "name": "SuperMan" }
]

This is is my returned value from my method:

var myReturnedValues = [
  [ [ "2", "4" ] ],
  [ [ "5", "5" ] ],
  [ [ "1", "3" ] ],
  [ [ "4", "3" ] ]
]

My output needs to be like this:

var myReturnedValues = [
  [ [ "Golem", "SpiderMan" ] ],
  [ [ "Archer", "Archer" ] ],
  [ [ "PEKKA", "Vigilante" ] ],
  [ [ "SpiderMan", "Vigilante" ] ]
]

What am trying to do here is I have to compare staticValues and myReturnedValues and return names with respect to their ids instead of returning only ids alone. I tried some more ways with underscore.js but failed. Can someone give me idea about that?

This is how my method looks like:

var staticValues = [ /* here I have the whole static data */ ];

$scope.getCategories = function() {
    var myReturnedValues = mainSteps.map(x => [x.steps.map(y => y.category)]);
    return myReturnedValues;
}

Code After edited ,

$scope.getCategories =function(){
                	 var myReturnedValues =mainSteps.map(x => [x.steps.map(y => y.category+"\n"+"\n"+"\n")]);
                	 //return myReturnedValues;
                	 console.log('meeee before',angular.toJson(myReturnedValues));
                	 newval = {};
                	 $.each(staticValues ,function(i,v) {
                		 console.log('meeee staticCategories',angular.toJson(staticValues )); 
                	   newval[v.id] = v.name;
                	 });


                	 $.each(myReturnedValues,function(i,v){
                	    $.each(v[0],function(x,t){
                	     myReturnedValues[i][0][x] = newval[t];
                	   });
                	 });
                	 console.log('meeee after',angular.toJson(myReturnedValues));
                	 return myReturnedValues;
                 }

3 Answers 3

2

Start by converting staticValues to a key => value object:

names = {}
staticValues.forEach(obj => names[obj.id] = obj.name);

Then iterate myReturedValues and replace ids with names as you go:

var staticValues = [
  { "id": "1", "name": "PEKKA" },
  { "id": "2", "name": "Golem" },
  { "id": "3", "name": "Vigilane" },
  { "id": "4", "name": "SpiderMan" },
  { "id": "5", "name": "Archer" },
  { "id": "6", "name": "SuperMan" }
]

var myReturnedValues = [
  [ [ "2", "4" ] ],
  [ [ "5", "5" ] ],
  [ [ "1", "3" ] ],
  [ [ "4", "3" ] ]
]

names = {}
staticValues.forEach(x => names[x.id] = x.name)

res = myReturnedValues.map(sub =>   
    [sub[0].map(id => names[id])]
)

console.log(res)

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

Comments

1

Try the following loops:

newval = {};
$.each(staticValues,function(i,v) {
  newval[v.id] = v.name;
});

var myReturedValues = [
  [ [ "2", "4" ] ],
  [ [ "5", "5" ] ],
  [ [ "1", "3" ] ],
  [ [ "4", "3" ] ]
];
$.each(myReturedValues,function(i,v){
   $.each(v[0],function(x,t){
    myReturedValues[i][0][x] = newval[t];
  });
});

demo: https://jsfiddle.net/cgk478g8/

16 Comments

I tried that now, and my output is [[[null,null]],[[null,null]],[[null,null]]] ,
as you can see in my jsfiddle is working, you must have another problem in you code
why are you wrapping a array in a array [ [ "2", "4" ] ],?
let me check again,
I edited my code. have a look at that , Still am getting null as output
|
1

With underscore you can try like this:

var staticValues = [
  { "id": "1", "name": "PEKKA" },
  { "id": "2", "name": "Golem" },
  { "id": "3", "name": "Vigilane" },
  { "id": "4", "name": "SpiderMan" },
  { "id": "5", "name": "Archer" },
  { "id": "6", "name": "SuperMan" }
];

var myReturedValues = [
  [ [ "2", "4" ] ],
  [ [ "5", "5" ] ],
  [ [ "1", "3" ] ],
  [ [ "4", "3" ] ]
];
var data = _.chain(myReturedValues).map(function(d) {
  //map the myReturedValues
  return d[0].map(function(id) {
    //use underscore to search in the staticValues and return name
    return _.find(staticValues, function(svalue) {
      return svalue.id == id
    }).name
  });
}).value();

console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>

1 Comment

,When i'm trying this it say's "name undefined " !!!! what may be the problem buddy ?

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.