HattrickNZ
HattrickNZ

Reputation: 4643

How do I copy an array of arrays of objects and be able to manipulate the copied array and not affect the original array

This is my dataset:

dfg= [[{"key":"Product_Description","value":"$49.95 Plan"},{"key":">20MB/30","value":44555},{"key":">200MB/30","value":36599},{"key":">2048MB/30","value":16173},{"key":">5120MB/30","value":6380},{"key":">10240MB/30","value":2178}],[{"key":"Product_Description","value":"$39.95 Plan"},{"key":">20MB/30","value":54832},{"key":">200MB/30","value":42988},{"key":">2048MB/30","value":14826},{"key":">5120MB/30","value":4742},{"key":">10240MB/30","value":1567}]]

I want to pass it through this function so that I change the values in the above into percentage of the total value for 'Product Description'

//funtion to format data for percent values 
function format_data_to_percent(d){

var data = d
data.map(function(a){ 
    var sum = a.slice(1).map(function(a){ return a.value; }).reduce(function(a,b){return a*1+b*1},0); 
    //console.log("sum",sum); 

    a.slice(1).filter(function(a){
        //console.log(a)
        a.value=a.value/sum
        //console.log(a)
    }); 

    return a;
})


//START TEST 
// create a test case for it // make this better
vals=data_p[0].slice(1).map(function(a){ return a.value; })
//["11", "33", "10", "55", "10"]
sum = data_p[0].slice(1).map(function(a){ return a.value; }).reduce(function(a, b){return a*1+b*1},0);
//119
vals2=vals.map(function(a){ return a/sum})
//[0.09243697478991597, 0.2773109243697479, 0.08403361344537816, 0.46218487394957986, 0.08403361344537816]


console.log("test1--> ",data[0][1].value == vals2[0])
console.log("test2--> ",data[0][2].value == vals2[1])
console.log("test3--> ",data[0][3].value == vals2[2])
console.log("test4--> ",data[0][4].value == vals2[3])
console.log("test5--> ",data[0][5].value == vals2[4])
//END TEST 

    return data
}

dfg2=format_data_to_percent(dfg) //format my dataset 

and this is what I get which is what I want

[[{"key":"Product_Description","value":"$49.95 Plan"},{"key":">20MB/30","value":0.42078670255465833},{"key":">200MB/30","value":0.345648581007697},{"key":">2048MB/30","value":0.15274118147046323},{"key":">5120MB/30","value":0.06025404920432545},{"key":">10240MB/30","value":0.02056948576285593}],[{"key":"Product_Description","value":"$39.95 Plan"},{"key":">20MB/30","value":0.4609474170904964},{"key":">200MB/30","value":0.3613803539153461},{"key":">2048MB/30","value":0.12463536631499307},{"key":">5120MB/30","value":0.03986381404732882},{"key":">10240MB/30","value":0.013173048631835568}]]

but dfg is also equal to df2, I want dfg to remain as it was with the numbers and dfg2 to have the percentages.

What I am doing my not be best practice. I understand, although not as complete as I would like the objects in the array are all the same reference. But is there a way I can achieve dfg with the number values and dfg2 with the percentage values?

I have come accross this, and this copies the array by slicing the original. I have tried this, but it does not work in my case, maybe in there example there is no objects, whereas I have objects inside my array.

Edit1 possible duplication here but this is more java. This might be what I want

when you assign an array directly, it is a reference assignment in javascript. This means that any changes will be reflected in both. To copy an array, you will need to call array.slice().

note: this will still be an assignment on a multi-dimensional array, so you will need to write something recursive to copy an array that has more than one dimension (at any element, e.g. [1, 2, ['some', 'inner', 'array'], 3])


EDIT2

tks to anser below: just for ref here is full log from my browser

dfg
[[Object { key="Product_Description",  value="$100.83 Business Select Plan excl GST"}, Object { key=">20MB/30",  value=1480}, Object { key=">200MB/30",  value=1234}, 3 more...], [Object { key="Product_Description",  value="$119.95 Business Share Everything Plan"}, Object { key=">20MB/30",  value=66}, Object { key=">200MB/30",  value=57}, 3 more...]]
var arr2 = []
undefined
arr2.push(JSON.parse(JSON.stringify(dfg[0])))
1
arr2.push(JSON.parse(JSON.stringify(dfg[1])))
2
arr2
[[Object { key="Product_Description",  value="$100.83 Business Select Plan excl GST"}, Object { key=">20MB/30",  value=1480}, Object { key=">200MB/30",  value=1234}, 3 more...], [Object { key="Product_Description",  value="$119.95 Business Share Everything Plan"}, Object { key=">20MB/30",  value=66}, Object { key=">200MB/30",  value=57}, 3 more...]]
//funtion to format data for percent values  func...alue == vals2[4])   //END TEST              return data }
undefined
var arr3 = format_data_to_percent(arr2)
test1--> true
test2--> true
test3--> true
test4--> true
test5--> true
undefined
arr3
[[Object { key="Product_Description",  value="$100.83 Business Select Plan excl GST"}, Object { key=">20MB/30",  value=0.37336024217961655}, Object { key=">200MB/30",  value=0.3113017154389506}, 3 more...], [Object { key="Product_Description",  value="$119.95 Business Share Everything Plan"}, Object { key=">20MB/30",  value=0.3815028901734104}, Object { key=">200MB/30",  value=0.32947976878612717}, 3 more...]]
dfg
[[Object { key="Product_Description",  value="$100.83 Business Select Plan excl GST"}, Object { key=">20MB/30",  value=1480}, Object { key=">200MB/30",  value=1234}, 3 more...], [Object { key="Product_Description",  value="$119.95 Business Share Everything Plan"}, Object { key=">20MB/30",  value=66}, Object { key=">200MB/30",  value=57}, 3 more...]]

Upvotes: 1

Views: 833

Answers (2)

NoobSter
NoobSter

Reputation: 1160

You can clone the object of the array.

for instance:

var arr = [{'one': 1}, {'two': 2}];

var arr2 = [];

arr2.push(JSON.parse(JSON.stringify(arr[0])));

arr2 will now have an independent copy of arr[0], using JSON.parse w/ JSON.stringify. You could use a for loop, for each, or map to copy all objects in the original array.

Upvotes: 1

Lee Brindley
Lee Brindley

Reputation: 6472

Clone the array? That way you'll have a copy of the original array which is not an array of references to the original.

If you're using jquery then you can use .extend

var array_1 = [{},{},{}];

var array_2 = $.extend({}, true, array_1); // deep clone 

Or you could use some new JavaScript. Although this only performs a shallow copy so is probably not fit for your use-case, I'm on the mobile app so can't play with your code..

var array_2 = Object.assign({}, array_1); 

Upvotes: 3

Related Questions