Original object referenced as I apply .map function in JavaScript
up vote
0
down vote
favorite
I am reading data from a json file and storing it in an object in javascript , I am using d3js library to read the file.
This is what the raw data looks like in data.json file :
{
"bitcoin": [
{
"24h_vol": null,
"date": "12/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": null,
"date": "13/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": "0",
"date": "14/5/2013",
"market_cap": "1500517590",
"price_usd": "135.3"
},...]
"bitcoin_cash": [
{
"24h_vol": null,
"date": "12/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": null,
"date": "13/5/2013",
"market_cap": null,
"price_usd": null
},...]
}
I read this and then filter some of the null entries out and also parse the date and Integer values respectively, This is the code for the same:
//Get data
d3.json("data/coins.json").then((data) => {
console.log("original data", data.bitcoin);
/*---
original data (1633) [{…}, , …]
[0 … 99]
0: {24h_vol: null, date: "12/5/2013", market_cap: null, price_usd: null}
1: {24h_vol: null, date: "13/5/2013", market_cap: null, price_usd: null}
2: {24h_vol: "0", date: Tue May 14 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1500517590, price_usd: 135.3}
...
--------*/
//Selector listener
$("#coin-select").change(function() {
var coinType =this.value;
var coinData = data[coinType];
var cleanData = coinData.filter((d) => {
return (d.price_usd)
}).map((d) => {
d.price_usd =+ d.price_usd;
d.market_cap =+ d.market_cap;
d.date = parsedDate(d.date);
return d;
});
console.log("cleanData", cleanData)
/*------
cleanData (1631) [{…}, {…}, , …]
[0 … 99]
0: {24h_vol: "0", date: Tue May 14 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1500517590, price_usd: 135.3}
1: {24h_vol: "0", date: Wed May 15 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1575032004, price_usd: 141.96}
---*/
update(cleanData);
});
//Default to bitcoin
$('#coin-select')
.val('bitcoin')
.trigger('change');
});
As you can see the console output , the original data has parsed values for date,market_cap and price_usd too, not sure why this is happening.
Thanks for you time.
PS: this doesn't only happens in chrome as suggested in the question : Is Chrome's JavaScript console lazy about evaluating arrays?
javascript ecmascript-6
add a comment |
up vote
0
down vote
favorite
I am reading data from a json file and storing it in an object in javascript , I am using d3js library to read the file.
This is what the raw data looks like in data.json file :
{
"bitcoin": [
{
"24h_vol": null,
"date": "12/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": null,
"date": "13/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": "0",
"date": "14/5/2013",
"market_cap": "1500517590",
"price_usd": "135.3"
},...]
"bitcoin_cash": [
{
"24h_vol": null,
"date": "12/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": null,
"date": "13/5/2013",
"market_cap": null,
"price_usd": null
},...]
}
I read this and then filter some of the null entries out and also parse the date and Integer values respectively, This is the code for the same:
//Get data
d3.json("data/coins.json").then((data) => {
console.log("original data", data.bitcoin);
/*---
original data (1633) [{…}, , …]
[0 … 99]
0: {24h_vol: null, date: "12/5/2013", market_cap: null, price_usd: null}
1: {24h_vol: null, date: "13/5/2013", market_cap: null, price_usd: null}
2: {24h_vol: "0", date: Tue May 14 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1500517590, price_usd: 135.3}
...
--------*/
//Selector listener
$("#coin-select").change(function() {
var coinType =this.value;
var coinData = data[coinType];
var cleanData = coinData.filter((d) => {
return (d.price_usd)
}).map((d) => {
d.price_usd =+ d.price_usd;
d.market_cap =+ d.market_cap;
d.date = parsedDate(d.date);
return d;
});
console.log("cleanData", cleanData)
/*------
cleanData (1631) [{…}, {…}, , …]
[0 … 99]
0: {24h_vol: "0", date: Tue May 14 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1500517590, price_usd: 135.3}
1: {24h_vol: "0", date: Wed May 15 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1575032004, price_usd: 141.96}
---*/
update(cleanData);
});
//Default to bitcoin
$('#coin-select')
.val('bitcoin')
.trigger('change');
});
As you can see the console output , the original data has parsed values for date,market_cap and price_usd too, not sure why this is happening.
Thanks for you time.
PS: this doesn't only happens in chrome as suggested in the question : Is Chrome's JavaScript console lazy about evaluating arrays?
javascript ecmascript-6
Objects are passed around by Ref, not Val. If you want to preserve the original, you'll need to perform your operation in a way which creates a new object, e.g. you could spread the object.map(d => ({...d, price_usd: d.price_usd + d.price_usd, market_cap: d.market_cap + d.market_cap, date: parsedDate(d.date)}))
– Paul S.
Nov 9 at 21:59
1
In your map, create a new object rather than modifying and returning the current one.
– Kevin B
Nov 9 at 22:09
Possible duplicate of Is Chrome's JavaScript console lazy about evaluating arrays?
– vnt
Nov 9 at 22:18
@vnt It doesn't seem like as it happens on other browsers too
– Snedden27
Nov 11 at 15:55
@KevinB that seem to work , but I still don't understand it completely , doens't have this line in the code: var coinData = data[coinType]; segregate the 'data' object from the 'coinData' object. don't seem to understand why using 'coinData' in map function object changes the original data object
– Snedden27
Nov 11 at 16:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am reading data from a json file and storing it in an object in javascript , I am using d3js library to read the file.
This is what the raw data looks like in data.json file :
{
"bitcoin": [
{
"24h_vol": null,
"date": "12/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": null,
"date": "13/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": "0",
"date": "14/5/2013",
"market_cap": "1500517590",
"price_usd": "135.3"
},...]
"bitcoin_cash": [
{
"24h_vol": null,
"date": "12/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": null,
"date": "13/5/2013",
"market_cap": null,
"price_usd": null
},...]
}
I read this and then filter some of the null entries out and also parse the date and Integer values respectively, This is the code for the same:
//Get data
d3.json("data/coins.json").then((data) => {
console.log("original data", data.bitcoin);
/*---
original data (1633) [{…}, , …]
[0 … 99]
0: {24h_vol: null, date: "12/5/2013", market_cap: null, price_usd: null}
1: {24h_vol: null, date: "13/5/2013", market_cap: null, price_usd: null}
2: {24h_vol: "0", date: Tue May 14 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1500517590, price_usd: 135.3}
...
--------*/
//Selector listener
$("#coin-select").change(function() {
var coinType =this.value;
var coinData = data[coinType];
var cleanData = coinData.filter((d) => {
return (d.price_usd)
}).map((d) => {
d.price_usd =+ d.price_usd;
d.market_cap =+ d.market_cap;
d.date = parsedDate(d.date);
return d;
});
console.log("cleanData", cleanData)
/*------
cleanData (1631) [{…}, {…}, , …]
[0 … 99]
0: {24h_vol: "0", date: Tue May 14 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1500517590, price_usd: 135.3}
1: {24h_vol: "0", date: Wed May 15 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1575032004, price_usd: 141.96}
---*/
update(cleanData);
});
//Default to bitcoin
$('#coin-select')
.val('bitcoin')
.trigger('change');
});
As you can see the console output , the original data has parsed values for date,market_cap and price_usd too, not sure why this is happening.
Thanks for you time.
PS: this doesn't only happens in chrome as suggested in the question : Is Chrome's JavaScript console lazy about evaluating arrays?
javascript ecmascript-6
I am reading data from a json file and storing it in an object in javascript , I am using d3js library to read the file.
This is what the raw data looks like in data.json file :
{
"bitcoin": [
{
"24h_vol": null,
"date": "12/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": null,
"date": "13/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": "0",
"date": "14/5/2013",
"market_cap": "1500517590",
"price_usd": "135.3"
},...]
"bitcoin_cash": [
{
"24h_vol": null,
"date": "12/5/2013",
"market_cap": null,
"price_usd": null
},
{
"24h_vol": null,
"date": "13/5/2013",
"market_cap": null,
"price_usd": null
},...]
}
I read this and then filter some of the null entries out and also parse the date and Integer values respectively, This is the code for the same:
//Get data
d3.json("data/coins.json").then((data) => {
console.log("original data", data.bitcoin);
/*---
original data (1633) [{…}, , …]
[0 … 99]
0: {24h_vol: null, date: "12/5/2013", market_cap: null, price_usd: null}
1: {24h_vol: null, date: "13/5/2013", market_cap: null, price_usd: null}
2: {24h_vol: "0", date: Tue May 14 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1500517590, price_usd: 135.3}
...
--------*/
//Selector listener
$("#coin-select").change(function() {
var coinType =this.value;
var coinData = data[coinType];
var cleanData = coinData.filter((d) => {
return (d.price_usd)
}).map((d) => {
d.price_usd =+ d.price_usd;
d.market_cap =+ d.market_cap;
d.date = parsedDate(d.date);
return d;
});
console.log("cleanData", cleanData)
/*------
cleanData (1631) [{…}, {…}, , …]
[0 … 99]
0: {24h_vol: "0", date: Tue May 14 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1500517590, price_usd: 135.3}
1: {24h_vol: "0", date: Wed May 15 2013 00:00:00 GMT-0400 (Eastern Daylight Time), market_cap: 1575032004, price_usd: 141.96}
---*/
update(cleanData);
});
//Default to bitcoin
$('#coin-select')
.val('bitcoin')
.trigger('change');
});
As you can see the console output , the original data has parsed values for date,market_cap and price_usd too, not sure why this is happening.
Thanks for you time.
PS: this doesn't only happens in chrome as suggested in the question : Is Chrome's JavaScript console lazy about evaluating arrays?
javascript ecmascript-6
javascript ecmascript-6
edited Nov 11 at 15:57
asked Nov 9 at 21:52
Snedden27
87942045
87942045
Objects are passed around by Ref, not Val. If you want to preserve the original, you'll need to perform your operation in a way which creates a new object, e.g. you could spread the object.map(d => ({...d, price_usd: d.price_usd + d.price_usd, market_cap: d.market_cap + d.market_cap, date: parsedDate(d.date)}))
– Paul S.
Nov 9 at 21:59
1
In your map, create a new object rather than modifying and returning the current one.
– Kevin B
Nov 9 at 22:09
Possible duplicate of Is Chrome's JavaScript console lazy about evaluating arrays?
– vnt
Nov 9 at 22:18
@vnt It doesn't seem like as it happens on other browsers too
– Snedden27
Nov 11 at 15:55
@KevinB that seem to work , but I still don't understand it completely , doens't have this line in the code: var coinData = data[coinType]; segregate the 'data' object from the 'coinData' object. don't seem to understand why using 'coinData' in map function object changes the original data object
– Snedden27
Nov 11 at 16:25
add a comment |
Objects are passed around by Ref, not Val. If you want to preserve the original, you'll need to perform your operation in a way which creates a new object, e.g. you could spread the object.map(d => ({...d, price_usd: d.price_usd + d.price_usd, market_cap: d.market_cap + d.market_cap, date: parsedDate(d.date)}))
– Paul S.
Nov 9 at 21:59
1
In your map, create a new object rather than modifying and returning the current one.
– Kevin B
Nov 9 at 22:09
Possible duplicate of Is Chrome's JavaScript console lazy about evaluating arrays?
– vnt
Nov 9 at 22:18
@vnt It doesn't seem like as it happens on other browsers too
– Snedden27
Nov 11 at 15:55
@KevinB that seem to work , but I still don't understand it completely , doens't have this line in the code: var coinData = data[coinType]; segregate the 'data' object from the 'coinData' object. don't seem to understand why using 'coinData' in map function object changes the original data object
– Snedden27
Nov 11 at 16:25
Objects are passed around by Ref, not Val. If you want to preserve the original, you'll need to perform your operation in a way which creates a new object, e.g. you could spread the object
.map(d => ({...d, price_usd: d.price_usd + d.price_usd, market_cap: d.market_cap + d.market_cap, date: parsedDate(d.date)}))– Paul S.
Nov 9 at 21:59
Objects are passed around by Ref, not Val. If you want to preserve the original, you'll need to perform your operation in a way which creates a new object, e.g. you could spread the object
.map(d => ({...d, price_usd: d.price_usd + d.price_usd, market_cap: d.market_cap + d.market_cap, date: parsedDate(d.date)}))– Paul S.
Nov 9 at 21:59
1
1
In your map, create a new object rather than modifying and returning the current one.
– Kevin B
Nov 9 at 22:09
In your map, create a new object rather than modifying and returning the current one.
– Kevin B
Nov 9 at 22:09
Possible duplicate of Is Chrome's JavaScript console lazy about evaluating arrays?
– vnt
Nov 9 at 22:18
Possible duplicate of Is Chrome's JavaScript console lazy about evaluating arrays?
– vnt
Nov 9 at 22:18
@vnt It doesn't seem like as it happens on other browsers too
– Snedden27
Nov 11 at 15:55
@vnt It doesn't seem like as it happens on other browsers too
– Snedden27
Nov 11 at 15:55
@KevinB that seem to work , but I still don't understand it completely , doens't have this line in the code: var coinData = data[coinType]; segregate the 'data' object from the 'coinData' object. don't seem to understand why using 'coinData' in map function object changes the original data object
– Snedden27
Nov 11 at 16:25
@KevinB that seem to work , but I still don't understand it completely , doens't have this line in the code: var coinData = data[coinType]; segregate the 'data' object from the 'coinData' object. don't seem to understand why using 'coinData' in map function object changes the original data object
– Snedden27
Nov 11 at 16:25
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Try something like this:
$("#coin-select").change(function() {
var coinType = this.value;
var coinData = data[coinType];
var cleanData = coinData.filter(x => x.price_usd)
.map(d => ({
price_usd: d.price_usd =+ d.price_usd,
market_cap: d.market_cap =+ d.market_cap,
date: parsedDate(d.date)
}));
update(cleanData);
});
This makes sure you return a new object from your map so you do not mutate the original and also cleans up some of the explicit returns you had which are not needed.
Not sure how you are using the arrow function for .map. copied letter by letter seems to give a syntax error
– Snedden27
Nov 11 at 16:18
There was an extra comma in the object literal. Try again
– Akrion
Nov 11 at 18:03
yes it does but there seems to be a missing ending parenthesis too , can you change that and also if possible can you make the new object creation more explicit May its just my opinion but it might a little more discern-able for ecmascript6 noobs like me
– Snedden27
Nov 12 at 0:55
Updated it. Sorry on mobile ...
– Akrion
Nov 12 at 0:58
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Try something like this:
$("#coin-select").change(function() {
var coinType = this.value;
var coinData = data[coinType];
var cleanData = coinData.filter(x => x.price_usd)
.map(d => ({
price_usd: d.price_usd =+ d.price_usd,
market_cap: d.market_cap =+ d.market_cap,
date: parsedDate(d.date)
}));
update(cleanData);
});
This makes sure you return a new object from your map so you do not mutate the original and also cleans up some of the explicit returns you had which are not needed.
Not sure how you are using the arrow function for .map. copied letter by letter seems to give a syntax error
– Snedden27
Nov 11 at 16:18
There was an extra comma in the object literal. Try again
– Akrion
Nov 11 at 18:03
yes it does but there seems to be a missing ending parenthesis too , can you change that and also if possible can you make the new object creation more explicit May its just my opinion but it might a little more discern-able for ecmascript6 noobs like me
– Snedden27
Nov 12 at 0:55
Updated it. Sorry on mobile ...
– Akrion
Nov 12 at 0:58
add a comment |
up vote
1
down vote
accepted
Try something like this:
$("#coin-select").change(function() {
var coinType = this.value;
var coinData = data[coinType];
var cleanData = coinData.filter(x => x.price_usd)
.map(d => ({
price_usd: d.price_usd =+ d.price_usd,
market_cap: d.market_cap =+ d.market_cap,
date: parsedDate(d.date)
}));
update(cleanData);
});
This makes sure you return a new object from your map so you do not mutate the original and also cleans up some of the explicit returns you had which are not needed.
Not sure how you are using the arrow function for .map. copied letter by letter seems to give a syntax error
– Snedden27
Nov 11 at 16:18
There was an extra comma in the object literal. Try again
– Akrion
Nov 11 at 18:03
yes it does but there seems to be a missing ending parenthesis too , can you change that and also if possible can you make the new object creation more explicit May its just my opinion but it might a little more discern-able for ecmascript6 noobs like me
– Snedden27
Nov 12 at 0:55
Updated it. Sorry on mobile ...
– Akrion
Nov 12 at 0:58
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Try something like this:
$("#coin-select").change(function() {
var coinType = this.value;
var coinData = data[coinType];
var cleanData = coinData.filter(x => x.price_usd)
.map(d => ({
price_usd: d.price_usd =+ d.price_usd,
market_cap: d.market_cap =+ d.market_cap,
date: parsedDate(d.date)
}));
update(cleanData);
});
This makes sure you return a new object from your map so you do not mutate the original and also cleans up some of the explicit returns you had which are not needed.
Try something like this:
$("#coin-select").change(function() {
var coinType = this.value;
var coinData = data[coinType];
var cleanData = coinData.filter(x => x.price_usd)
.map(d => ({
price_usd: d.price_usd =+ d.price_usd,
market_cap: d.market_cap =+ d.market_cap,
date: parsedDate(d.date)
}));
update(cleanData);
});
This makes sure you return a new object from your map so you do not mutate the original and also cleans up some of the explicit returns you had which are not needed.
edited Nov 12 at 0:57
answered Nov 9 at 22:16
Akrion
8,79411223
8,79411223
Not sure how you are using the arrow function for .map. copied letter by letter seems to give a syntax error
– Snedden27
Nov 11 at 16:18
There was an extra comma in the object literal. Try again
– Akrion
Nov 11 at 18:03
yes it does but there seems to be a missing ending parenthesis too , can you change that and also if possible can you make the new object creation more explicit May its just my opinion but it might a little more discern-able for ecmascript6 noobs like me
– Snedden27
Nov 12 at 0:55
Updated it. Sorry on mobile ...
– Akrion
Nov 12 at 0:58
add a comment |
Not sure how you are using the arrow function for .map. copied letter by letter seems to give a syntax error
– Snedden27
Nov 11 at 16:18
There was an extra comma in the object literal. Try again
– Akrion
Nov 11 at 18:03
yes it does but there seems to be a missing ending parenthesis too , can you change that and also if possible can you make the new object creation more explicit May its just my opinion but it might a little more discern-able for ecmascript6 noobs like me
– Snedden27
Nov 12 at 0:55
Updated it. Sorry on mobile ...
– Akrion
Nov 12 at 0:58
Not sure how you are using the arrow function for .map. copied letter by letter seems to give a syntax error
– Snedden27
Nov 11 at 16:18
Not sure how you are using the arrow function for .map. copied letter by letter seems to give a syntax error
– Snedden27
Nov 11 at 16:18
There was an extra comma in the object literal. Try again
– Akrion
Nov 11 at 18:03
There was an extra comma in the object literal. Try again
– Akrion
Nov 11 at 18:03
yes it does but there seems to be a missing ending parenthesis too , can you change that and also if possible can you make the new object creation more explicit May its just my opinion but it might a little more discern-able for ecmascript6 noobs like me
– Snedden27
Nov 12 at 0:55
yes it does but there seems to be a missing ending parenthesis too , can you change that and also if possible can you make the new object creation more explicit May its just my opinion but it might a little more discern-able for ecmascript6 noobs like me
– Snedden27
Nov 12 at 0:55
Updated it. Sorry on mobile ...
– Akrion
Nov 12 at 0:58
Updated it. Sorry on mobile ...
– Akrion
Nov 12 at 0:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53233694%2foriginal-object-referenced-as-i-apply-map-function-in-javascript%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Objects are passed around by Ref, not Val. If you want to preserve the original, you'll need to perform your operation in a way which creates a new object, e.g. you could spread the object
.map(d => ({...d, price_usd: d.price_usd + d.price_usd, market_cap: d.market_cap + d.market_cap, date: parsedDate(d.date)}))– Paul S.
Nov 9 at 21:59
1
In your map, create a new object rather than modifying and returning the current one.
– Kevin B
Nov 9 at 22:09
Possible duplicate of Is Chrome's JavaScript console lazy about evaluating arrays?
– vnt
Nov 9 at 22:18
@vnt It doesn't seem like as it happens on other browsers too
– Snedden27
Nov 11 at 15:55
@KevinB that seem to work , but I still don't understand it completely , doens't have this line in the code: var coinData = data[coinType]; segregate the 'data' object from the 'coinData' object. don't seem to understand why using 'coinData' in map function object changes the original data object
– Snedden27
Nov 11 at 16:25