-2
var jsonObj = [
     {
       "key1": "value1",
       "key2": "value2", 
       "key3​": "value3" 
     }, 
     {
       "key1": "value1", 
       "key2": "value2",
       "key3​": "value3" 
     }
    ];

I want to update all the values of all the objects. Here the keys and values are dynamic not key1 and value1. Could you help to figure it out the requirement.

I want to make each value of each object to "changedvalue". So the final result after updating the jsonObj is

[
     {
       "key1": "changedvalue",
       "key2": "changedvalue", 
       "key3​": "changedvalue" 
     }, 
     {
       "key1": "changedvalue", 
       "key2": "changedvalue",
       "key3​": "changedvalue" 
     }
];
Phil
  • 157,677
  • 23
  • 242
  • 245
Sandeep sandy
  • 387
  • 7
  • 14
  • 2
    So how **exactly** do you want the result to look? What is it you want to change? – Phil Jul 26 '17 at 06:00
  • 2
    I'm voting to close this question as off-topic because question does not show any sign of effort. The problem statement is incomplete and unclear. – Rajesh Jul 26 '17 at 06:03
  • Thanks @weedoze for providing a solution, it's really appreciable for encouraging dumbs like me. Ya it's my wrong for missing of result what I need, that doesn't make any sense because I want to modify the value then what's the deal about what is the result. I will maintain it for sure as seniors suggested. – Sandeep sandy Jul 26 '17 at 06:30

4 Answers4

5

jsonObj is an array thus you first have to iterate this array

jsonObj.forEach(o => {...});

o is now an object. You have to iterate the keys/values of it

for(let k in o)

k is a key in the object. You can now alter the value

o[k] = 'WhateverYouNeed'

var jsonObj = [{
       "key1": "value1",
       "key2": "value2", 
       "key3": "value3" 
     }, 
     {
       "key1": "value1", 
       "key2": "value2",
       "key3": "value3" 
}];

jsonObj.forEach(o => {
  for(let k in o)
    o[k] = 'ChangedValue'
});

console.log(jsonObj);

References:

As stated, your structure is an array of objects and not JSON: Javascript object Vs JSON

Now breaking you problem into parts, you need to

  1. Update property of an object: How to set a Javascript object values dynamically?
  2. But you need to update them all: How do I loop through or enumerate a JavaScript object?
  3. But these objects are inside an array: Loop through an array in JavaScript
  4. But why do I have different mechanism to loop for Objects and Arrays? for cannot get keys but we can use for..in over arrays. Right? No, you should not. Why is using "for...in" with array iteration a bad idea?

ReadMe links:

Please refer following link and check browser compatibility as the solution will not work in older browsers. There are other ways to loop which are highlighted in above link. Refer compatibility for them as well before using them.

Community
  • 1
  • 1
Weedoze
  • 13,683
  • 1
  • 33
  • 63
  • Usually I would have requested to add explanation, but considering vagueness of question, I think we should try to explain the basics. – Rajesh Jul 26 '17 at 06:10
  • I would have answered but then it would have been duplication of content as your code is exactly what I'd do. So I updated your answer with reference links and a bit of explanation – Rajesh Jul 26 '17 at 06:23
  • Thanks for providing a solution, I'm not sure whether I done any wrong while using "jsonObj.forEach" as syntax error (in IE 11). But I achieved it with usual for loop. – Sandeep sandy Jul 26 '17 at 06:24
  • @Sandeepsandy please check all the links mentioned in the answer as they are meant to teach you. Also, my comment under question meant, you **should** try something and share some effort(*code/algo/ref links*) in your question. A partial question will receive downvotes and close votes, and will not attract good answers(*sometimes*). – Rajesh Jul 26 '17 at 06:31
  • But here I got a good answers from you and @weedoze, once again thanks for sharing worthful knowledge. I will surely maintain in future posts what you suggested. – Sandeep sandy Jul 26 '17 at 06:34
0

You might want to use a mixture of forEach and Object.keys

const jsonObj = [
  {
    "key1": "value1",
    "key2": "value2", 
    "key3​": "value3" 
  }, 
  {
    "key1": "value1", 
    "key2": "value2",
    "key3​": "value3" 
  }
];

jsonObj.forEach( obj => {
  Object.keys( obj ).forEach( key => {
    const value = obj[ key ];

    console.log( key, value );
  } );
} );
lumio
  • 7,428
  • 4
  • 40
  • 56
0

Here you go with a solution https://jsfiddle.net/Lha8xk34/

var jsonObj = [{
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}, {
  "key1": "value1",
  "key2": "value2",
  "key3": "value3"
}];

$.each(jsonObj, function(i) {
  $.each(jsonObj[i], function(key) {
    jsonObj[i][key] = "changed" + key;
  });
});

console.log(jsonObj);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Milan Chheda
  • 8,159
  • 3
  • 20
  • 35
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
-1

Try pinch

in your Case :

var jsonObj = [
     {
       "key1": "value1",
       "key2": "value2", 
       "key3​": "value3" 
     }, 
     {
       "key1": "value1", 
       "key2": "value2",
       "key3​": "value3" 
     }
    ];

pinch(data, "/key1/", function(path, key, value) {
  return (value === "value1") ? "updatedValue" : value;
});   
Arun pandian M
  • 862
  • 10
  • 17
  • *"Here the keys and values are dynamic not key1 and value1"* – Phil Jul 26 '17 at 06:07
  • @Phil even though its dynamic without looping or using general update it will update all the entires in the json which is not good method as per my understanding he want to update all the values in the json array based on the dynamic key (that should be known). – Arun pandian M Jul 26 '17 at 06:13
  • 2
    Why would you use an external library for a so simple requirement ? – Weedoze Jul 26 '17 at 06:15