-3

I have an object as,

var obj = [ 
            {
              "name": "a",
              "value": "1"
            },
            {
              "name": "b",
              "value": "2"
            },
            {
              "name": "c",
              "value": "3"
            }
          ]

I have a large object with more than 50 values. how can I change the value key using its name and what is the best looping technique for this.

I tried for loop for this like,

    for(i = 0; i < obj.length; i++) {
        if(obj[i].name == "b") {
            // some other functionality
            obj[i].value = "some_value";
        }
    }

But, it takes long time and sometimes for loop goes for next turn before if condition is executed.

Please explain how to solve it or is there any other looping technique

Harish Kommuri
  • 2,746
  • 1
  • 20
  • 26
  • 1
    You dont have an object, but it is an array of objects – Oxi Jan 22 '16 at 12:15
  • 1
    first you should put `var` before `i = 0` to make sure that `i` is not global. You could also add a `break`in the block condition if you don't have other value to change – Hacketo Jan 22 '16 at 12:17
  • 3
    _"and sometimes for loop goes for next turn before if condition is executed."_ What? Is your code asynchronous? – Cerbrus Jan 22 '16 at 12:21
  • Actually its a code of external plugin in cordova project – Harish Kommuri Jan 22 '16 at 12:26

5 Answers5

1

you can use forEach , but as far your hitting the performance its not best ,

you can use map but native for loop is fastest compared to map too

https://jsperf.com/native-map-versus-array-looping

Map , which runs on the each item of the array and return the new array

obj.map(function(item){
  if(item.name === "b"){
      item.value = "some_value"
   }
 return item;
})
Shushanth Pallegar
  • 2,654
  • 1
  • 12
  • 15
0

Map is your friend!

var obj = [ 
  { "name": "a", "value": "1" },
  { "name": "b", "value": "2" },
  { "name": "c", "value": "3" }
];

var newObj = obj.map((elm) => {
  if(elm.name === "b") elm.value = "some value";
  return elm;
});

Is this something like what you were looking for?

Neil Munro
  • 815
  • 4
  • 18
0

You can try this :

$(document).ready(function(){

  var obj = [ 
    {
      "name": "a",
      "value": "1"
    },
    {
      "name": "b",
      "value": "2"
    },
    {
      "name": "c",
      "value": "3"
    }
  ]

  for(i = 0; i < obj.length; i++) {
    (function(i){
      if(obj[i].name === "b") {
        console.log(obj[i].name);
        // some other functionality
        obj[i].value = "some_value";
      }
    })(i);
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
GameTag
  • 354
  • 1
  • 11
0

I think what you had was quite ok. As one of the comments stated, there was a mistake in the IF-statement which prevented it from being triggered.

I am not sure theres a faster way to proces the JSON object than the way you did. Here's a JSFiddle with some small changes.

function ReplaceValue(name, val) {
    for (i = 0; i < obj.length; i++) {
    if (obj[i].name == name) {
      // some other functionality
      obj[i].value = val;
      break;
    }
  }
    alert(JSON.stringify(obj, null, 2));
}
JTK
  • 181
  • 4
  • 12
-1

In lodash you can do something like this:

`

var obj = [ 
            {
              "name": "a",
              "value": "1"
            },
            {
              "name": "b",
              "value": "2"
            },
            {
              "name": "c",
              "value": "3"
            }
          ];        
 _.transform(arr, function(r, n){
   if(n.name == 'b'){
     r.push({name: n.name, value: 'some value'})}
   else{
     r.push(n)
   }
  })

`

Vikramaditya
  • 4,500
  • 4
  • 29
  • 43