Prem
Prem

Reputation: 652

Old JSON value Changed

I have two JSON objects columnsData and columns when assigning columnsData value to columns both values are changed.

var columnsData = [
      {id: "id"},      
      {id: "root_task_assignee"},
      {id: "root_task_id"},
      {id: "root_task_status"},
      {id: "root_task_tracker"},
      {id: "rt_category"},
      {id: "rt_priority"},
      {id: "rt_subject"},
]
var columns = [];

using the below function I assigned the columnsData value to columns object, and also added some additional fields

for(i = 0;i < columnsData.length; i++){
    columns[i] = columnsData[i];
    columns[i]["name"] = columnsData[i]["name"] || columnsData[i]["id"];
    columns[i]["type"] = columnsData[i]["id"]["type"] || "string";
} 

but after assigning both have the same values. How the old JSON columnsData value was changed? is there any other way to assign values

Upvotes: 0

Views: 63

Answers (3)

khushboo29
khushboo29

Reputation: 916

If it is required to keep original array pure (unchanged) we should use map method of array.

var columnsData = [
  {id: "id"},      
  {id: "root_task_assignee"},
  {id: "root_task_id"},
  {id: "root_task_status"},
  {id: "root_task_tracker"},
  {id: "rt_category"},
  {id: "rt_priority"},
  {id: "rt_subject"},
]

 var columns = columnsData.map(function(obj){ 
    var rObj = {};
    rObj[obj.key] = obj.value;
    rObj["name"] = obj.value;
    .....
    return rObj;
});

Logic can be added in map method to create new array as required. Hope it helps.

Upvotes: 1

Rayon
Rayon

Reputation: 36609

columns[i] = columnsData[i] will not copy content from one object to another but it will an reference of the columnsData[i]. As they are refereeing to same object, change in property of one object will affect the primary object which is being refereed.

Try this:

var columnsData = [{
  id: "id"
}, {
  id: "root_task_assignee"
}, {
  id: "root_task_id"
}, {
  id: "root_task_status"
}, {
  id: "root_task_tracker"
}, {
  id: "rt_category"
}, {
  id: "rt_priority"
}, {
  id: "rt_subject"
}, ]
var columns = [];
for (i = 0; i < columnsData.length; i++) {
  var obj = {};
  obj["name"] = columnsData[i]["name"] || columnsData[i]["id"];
  obj["type"] = columnsData[i]["id"]["type"] || "string";
  columns.push(obj)
}
alert(JSON.stringify(columns));
alert(JSON.stringify(columnsData));

Upvotes: 1

Amadan
Amadan

Reputation: 198436

columns[i] = columnsData[i] does not copy the data, it makes an additional reference to the same data.

For example, say you give Mr. Random Jones a nickname, "Cozy". If you give Cozy an envelope to hold, are you surprised if Mr. Jones is now holding an envelope too?

Same thing here. If you change columns[i], you are also changing columnsData[i], since they are the same object.

You would have to clone it if you wanted to have them be different. In this case, you just have to make a new object with id:

columns[i] = { id: columnsData[i].id };

In general, you would do well to find a nice clone function.

Upvotes: 2

Related Questions