How to append an object to an array based on the number of properties other than the specified properties?
up vote
2
down vote
favorite
For example , I have an array of objects like so :
[
{
"waterfallData.PopulationName":"Population 1",
"Deaths":-2333,
"Births":8786,
"open":0,
"close":6453
},
{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},
{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}
]
I want to add two(does not have to be two,if there is a "Extra Births"
then three) intermerdiate objects between each population like so
[
{
"waterfallData.PopulationName":"Population 1",
"Death":-2333,
"Births":8786,
"open":0,
"close":6453
},
{
"Deaths" : -1000,
"open" : 6453,
"close" : 5453
},
{
"Births" : 5000,
"open" : 5453,
"close : 10453
}
{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},
{
"Deaths" : -2000,
"open" : 10453,
"close" : 8453
},
{
"Births" : 500,
"open" : 8453,
"close" : 8953
}
{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}
]
So as you can see I want to add objects based on the number of properties other than the waterfallData.PopulationName
,open
and close
properties. Then, I want to assign open
and close
properties on each object based on the next "Deaths"
and "Births"
values.
For example , Population 1
starts with 6453 then I add two objects with the 1st object taking the next "Deaths"
value in Population 2
which is -1000 then I assign the open
property to be from the previous close
property of Population 1
and the close
property to be calculated by adding the assigned open
property to "Deaths"
's value. And same goes with the 2nd extra object where I assign the open
property to be the close
property of the previous object and the close
property to be calculated by adding open
with "Births"
's value.
How do I achieve this?
javascript arrays json
|
show 12 more comments
up vote
2
down vote
favorite
For example , I have an array of objects like so :
[
{
"waterfallData.PopulationName":"Population 1",
"Deaths":-2333,
"Births":8786,
"open":0,
"close":6453
},
{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},
{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}
]
I want to add two(does not have to be two,if there is a "Extra Births"
then three) intermerdiate objects between each population like so
[
{
"waterfallData.PopulationName":"Population 1",
"Death":-2333,
"Births":8786,
"open":0,
"close":6453
},
{
"Deaths" : -1000,
"open" : 6453,
"close" : 5453
},
{
"Births" : 5000,
"open" : 5453,
"close : 10453
}
{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},
{
"Deaths" : -2000,
"open" : 10453,
"close" : 8453
},
{
"Births" : 500,
"open" : 8453,
"close" : 8953
}
{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}
]
So as you can see I want to add objects based on the number of properties other than the waterfallData.PopulationName
,open
and close
properties. Then, I want to assign open
and close
properties on each object based on the next "Deaths"
and "Births"
values.
For example , Population 1
starts with 6453 then I add two objects with the 1st object taking the next "Deaths"
value in Population 2
which is -1000 then I assign the open
property to be from the previous close
property of Population 1
and the close
property to be calculated by adding the assigned open
property to "Deaths"
's value. And same goes with the 2nd extra object where I assign the open
property to be the close
property of the previous object and the close
property to be calculated by adding open
with "Births"
's value.
How do I achieve this?
javascript arrays json
Why not create using nested JSON?
– HymnZ
Nov 9 at 8:07
@HymnZ because I am using Amcharts and it does not support nested JSON data.
– Syed Ariff
Nov 9 at 8:09
You can try with Object.keys(your_array[1]) to create extra properties.. Just as @Grégory NEUT answered
– HymnZ
Nov 9 at 8:14
1
NB2: object properties are unordered, so the concept of "the number of properties after..." is unreliable. All you could say is "the number of properties other than...".
– trincot
Nov 9 at 8:21
1
Hey, no cheating. You changed the question from using properties such as 'State [Number]' to 'deatsh' and 'biths'. Trincot's deleted answer had a comment where he states that Object.keys is not to be guaranteed to come in any particular order so how will you know what's next and previous?
– HMR
Nov 9 at 8:43
|
show 12 more comments
up vote
2
down vote
favorite
up vote
2
down vote
favorite
For example , I have an array of objects like so :
[
{
"waterfallData.PopulationName":"Population 1",
"Deaths":-2333,
"Births":8786,
"open":0,
"close":6453
},
{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},
{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}
]
I want to add two(does not have to be two,if there is a "Extra Births"
then three) intermerdiate objects between each population like so
[
{
"waterfallData.PopulationName":"Population 1",
"Death":-2333,
"Births":8786,
"open":0,
"close":6453
},
{
"Deaths" : -1000,
"open" : 6453,
"close" : 5453
},
{
"Births" : 5000,
"open" : 5453,
"close : 10453
}
{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},
{
"Deaths" : -2000,
"open" : 10453,
"close" : 8453
},
{
"Births" : 500,
"open" : 8453,
"close" : 8953
}
{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}
]
So as you can see I want to add objects based on the number of properties other than the waterfallData.PopulationName
,open
and close
properties. Then, I want to assign open
and close
properties on each object based on the next "Deaths"
and "Births"
values.
For example , Population 1
starts with 6453 then I add two objects with the 1st object taking the next "Deaths"
value in Population 2
which is -1000 then I assign the open
property to be from the previous close
property of Population 1
and the close
property to be calculated by adding the assigned open
property to "Deaths"
's value. And same goes with the 2nd extra object where I assign the open
property to be the close
property of the previous object and the close
property to be calculated by adding open
with "Births"
's value.
How do I achieve this?
javascript arrays json
For example , I have an array of objects like so :
[
{
"waterfallData.PopulationName":"Population 1",
"Deaths":-2333,
"Births":8786,
"open":0,
"close":6453
},
{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},
{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}
]
I want to add two(does not have to be two,if there is a "Extra Births"
then three) intermerdiate objects between each population like so
[
{
"waterfallData.PopulationName":"Population 1",
"Death":-2333,
"Births":8786,
"open":0,
"close":6453
},
{
"Deaths" : -1000,
"open" : 6453,
"close" : 5453
},
{
"Births" : 5000,
"open" : 5453,
"close : 10453
}
{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},
{
"Deaths" : -2000,
"open" : 10453,
"close" : 8453
},
{
"Births" : 500,
"open" : 8453,
"close" : 8953
}
{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}
]
So as you can see I want to add objects based on the number of properties other than the waterfallData.PopulationName
,open
and close
properties. Then, I want to assign open
and close
properties on each object based on the next "Deaths"
and "Births"
values.
For example , Population 1
starts with 6453 then I add two objects with the 1st object taking the next "Deaths"
value in Population 2
which is -1000 then I assign the open
property to be from the previous close
property of Population 1
and the close
property to be calculated by adding the assigned open
property to "Deaths"
's value. And same goes with the 2nd extra object where I assign the open
property to be the close
property of the previous object and the close
property to be calculated by adding open
with "Births"
's value.
How do I achieve this?
javascript arrays json
javascript arrays json
edited Nov 16 at 7:01
asked Nov 9 at 7:57
Syed Ariff
3651317
3651317
Why not create using nested JSON?
– HymnZ
Nov 9 at 8:07
@HymnZ because I am using Amcharts and it does not support nested JSON data.
– Syed Ariff
Nov 9 at 8:09
You can try with Object.keys(your_array[1]) to create extra properties.. Just as @Grégory NEUT answered
– HymnZ
Nov 9 at 8:14
1
NB2: object properties are unordered, so the concept of "the number of properties after..." is unreliable. All you could say is "the number of properties other than...".
– trincot
Nov 9 at 8:21
1
Hey, no cheating. You changed the question from using properties such as 'State [Number]' to 'deatsh' and 'biths'. Trincot's deleted answer had a comment where he states that Object.keys is not to be guaranteed to come in any particular order so how will you know what's next and previous?
– HMR
Nov 9 at 8:43
|
show 12 more comments
Why not create using nested JSON?
– HymnZ
Nov 9 at 8:07
@HymnZ because I am using Amcharts and it does not support nested JSON data.
– Syed Ariff
Nov 9 at 8:09
You can try with Object.keys(your_array[1]) to create extra properties.. Just as @Grégory NEUT answered
– HymnZ
Nov 9 at 8:14
1
NB2: object properties are unordered, so the concept of "the number of properties after..." is unreliable. All you could say is "the number of properties other than...".
– trincot
Nov 9 at 8:21
1
Hey, no cheating. You changed the question from using properties such as 'State [Number]' to 'deatsh' and 'biths'. Trincot's deleted answer had a comment where he states that Object.keys is not to be guaranteed to come in any particular order so how will you know what's next and previous?
– HMR
Nov 9 at 8:43
Why not create using nested JSON?
– HymnZ
Nov 9 at 8:07
Why not create using nested JSON?
– HymnZ
Nov 9 at 8:07
@HymnZ because I am using Amcharts and it does not support nested JSON data.
– Syed Ariff
Nov 9 at 8:09
@HymnZ because I am using Amcharts and it does not support nested JSON data.
– Syed Ariff
Nov 9 at 8:09
You can try with Object.keys(your_array[1]) to create extra properties.. Just as @Grégory NEUT answered
– HymnZ
Nov 9 at 8:14
You can try with Object.keys(your_array[1]) to create extra properties.. Just as @Grégory NEUT answered
– HymnZ
Nov 9 at 8:14
1
1
NB2: object properties are unordered, so the concept of "the number of properties after..." is unreliable. All you could say is "the number of properties other than...".
– trincot
Nov 9 at 8:21
NB2: object properties are unordered, so the concept of "the number of properties after..." is unreliable. All you could say is "the number of properties other than...".
– trincot
Nov 9 at 8:21
1
1
Hey, no cheating. You changed the question from using properties such as 'State [Number]' to 'deatsh' and 'biths'. Trincot's deleted answer had a comment where he states that Object.keys is not to be guaranteed to come in any particular order so how will you know what's next and previous?
– HMR
Nov 9 at 8:43
Hey, no cheating. You changed the question from using properties such as 'State [Number]' to 'deatsh' and 'biths'. Trincot's deleted answer had a comment where he states that Object.keys is not to be guaranteed to come in any particular order so how will you know what's next and previous?
– HMR
Nov 9 at 8:43
|
show 12 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Crude.. but works
var newArr = ;
$x = [{
"waterfallData.PopulationName":"Population 1",
"Deaths":-2333,
"Births":8786,
"open":0,
"close":6453
},{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}];
$x.forEach((p,i)=>{
var current = $x[i]
newArr.push(current)
try {
var next = $x[i+1];
var start = current.open;
var end = current.close;
var states = Object.keys(current).sort().filter((k)=>{return (['waterfallData.PopulationName','open','close'].indexOf(k) < 0)})
for (var i=0;i<states.length;i++){
var state = states[i]
var tempObj = {}
tempObj[states[i]] = next[states[i]]
tempObj['open'] = end;
end += next[states[i]];
tempObj['close'] = end;
newArr.push(tempObj)
}
} catch (e) {
return false;
}
})
The code will look for all properties other than waterfallData.Popu..,open,close and treats them as states. If you have 10 properties, there will be 7 states excluding the above 3. Then the open and close values of these states are then calculated from the next element and pushed to new array newArr.
what is $x? It says ReferenceError $x is not defined.
– Syed Ariff
Nov 9 at 9:05
@SyedAriff .. a line got missed.. edited and updated.
– HymnZ
Nov 9 at 9:13
Now try to run your code where you switch the order ofDeaths
andBirths
in the$x
data:$x = [{... "Births":8786, "Deaths":-2333
The output will differ
– HMR
Nov 9 at 9:21
The point is that the output of your code relies on Object.keys (look at the first line) to return the keys in a specific order but this is not the case. The specification says that there is no guaranteed order of the keys.
– HMR
Nov 9 at 9:33
@HMR.. obviously, because by switching order of births & deaths, your representation itself changes.. to make it standardized.. I've added a sort to ensure consistency in the order. On a side note, where can I learn to code minimally like you. :) It's glorious.. :D
– HymnZ
Nov 9 at 10:07
|
show 2 more comments
up vote
0
down vote
I'll give it a try, you can specify what keys you need to be extra data but the original data must have values for those keys. It'll create the extra items in the order that the keys are provided:
const data = [{"waterfallData.PopulationName":"Population 1","Deaths":-2333,"Births":8786,"open":0,"close":6453},{"waterfallData.PopulationName":"Population 2","Deaths":-1000,"Births":5000,"open":0,"close":10453},{"waterfallData.PopulationName":"Population 3","Deaths":-2000,"Births":500,"open":0,"close":8953}];
const zip = (arr1, arr2) =>
[...new Array(Math.max(arr1.length, arr2.length))]
.map((_, i) => i)
.map((i) => [arr1[i], arr2[i]]);
const extras = (keys) => ([current, next]) =>
keys.map((key) => [key, next[key]]).reduce(
([result, lastClose], [key, nextValue]) => [
result.concat({
[key]: nextValue,
open: lastClose,
close: nextValue + lastClose,
}),
nextValue + lastClose,
],
[, current.close],
)[0];
const withExtras = (keys) => ([current, next]) =>
!next
? [current]
: [current].concat(extras(keys)([current, next]));
console.log(
zip(data, data.slice(1)) //having [[current,next],[current,next]...]
.map(withExtras(['Deaths', 'Births']))
.flatten(),
);
console.log(
'diffenrent order different result',
zip(data, data.slice(1))
.map(withExtras(['Births', 'Deaths']))
.flatten(),
);
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Crude.. but works
var newArr = ;
$x = [{
"waterfallData.PopulationName":"Population 1",
"Deaths":-2333,
"Births":8786,
"open":0,
"close":6453
},{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}];
$x.forEach((p,i)=>{
var current = $x[i]
newArr.push(current)
try {
var next = $x[i+1];
var start = current.open;
var end = current.close;
var states = Object.keys(current).sort().filter((k)=>{return (['waterfallData.PopulationName','open','close'].indexOf(k) < 0)})
for (var i=0;i<states.length;i++){
var state = states[i]
var tempObj = {}
tempObj[states[i]] = next[states[i]]
tempObj['open'] = end;
end += next[states[i]];
tempObj['close'] = end;
newArr.push(tempObj)
}
} catch (e) {
return false;
}
})
The code will look for all properties other than waterfallData.Popu..,open,close and treats them as states. If you have 10 properties, there will be 7 states excluding the above 3. Then the open and close values of these states are then calculated from the next element and pushed to new array newArr.
what is $x? It says ReferenceError $x is not defined.
– Syed Ariff
Nov 9 at 9:05
@SyedAriff .. a line got missed.. edited and updated.
– HymnZ
Nov 9 at 9:13
Now try to run your code where you switch the order ofDeaths
andBirths
in the$x
data:$x = [{... "Births":8786, "Deaths":-2333
The output will differ
– HMR
Nov 9 at 9:21
The point is that the output of your code relies on Object.keys (look at the first line) to return the keys in a specific order but this is not the case. The specification says that there is no guaranteed order of the keys.
– HMR
Nov 9 at 9:33
@HMR.. obviously, because by switching order of births & deaths, your representation itself changes.. to make it standardized.. I've added a sort to ensure consistency in the order. On a side note, where can I learn to code minimally like you. :) It's glorious.. :D
– HymnZ
Nov 9 at 10:07
|
show 2 more comments
up vote
1
down vote
accepted
Crude.. but works
var newArr = ;
$x = [{
"waterfallData.PopulationName":"Population 1",
"Deaths":-2333,
"Births":8786,
"open":0,
"close":6453
},{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}];
$x.forEach((p,i)=>{
var current = $x[i]
newArr.push(current)
try {
var next = $x[i+1];
var start = current.open;
var end = current.close;
var states = Object.keys(current).sort().filter((k)=>{return (['waterfallData.PopulationName','open','close'].indexOf(k) < 0)})
for (var i=0;i<states.length;i++){
var state = states[i]
var tempObj = {}
tempObj[states[i]] = next[states[i]]
tempObj['open'] = end;
end += next[states[i]];
tempObj['close'] = end;
newArr.push(tempObj)
}
} catch (e) {
return false;
}
})
The code will look for all properties other than waterfallData.Popu..,open,close and treats them as states. If you have 10 properties, there will be 7 states excluding the above 3. Then the open and close values of these states are then calculated from the next element and pushed to new array newArr.
what is $x? It says ReferenceError $x is not defined.
– Syed Ariff
Nov 9 at 9:05
@SyedAriff .. a line got missed.. edited and updated.
– HymnZ
Nov 9 at 9:13
Now try to run your code where you switch the order ofDeaths
andBirths
in the$x
data:$x = [{... "Births":8786, "Deaths":-2333
The output will differ
– HMR
Nov 9 at 9:21
The point is that the output of your code relies on Object.keys (look at the first line) to return the keys in a specific order but this is not the case. The specification says that there is no guaranteed order of the keys.
– HMR
Nov 9 at 9:33
@HMR.. obviously, because by switching order of births & deaths, your representation itself changes.. to make it standardized.. I've added a sort to ensure consistency in the order. On a side note, where can I learn to code minimally like you. :) It's glorious.. :D
– HymnZ
Nov 9 at 10:07
|
show 2 more comments
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Crude.. but works
var newArr = ;
$x = [{
"waterfallData.PopulationName":"Population 1",
"Deaths":-2333,
"Births":8786,
"open":0,
"close":6453
},{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}];
$x.forEach((p,i)=>{
var current = $x[i]
newArr.push(current)
try {
var next = $x[i+1];
var start = current.open;
var end = current.close;
var states = Object.keys(current).sort().filter((k)=>{return (['waterfallData.PopulationName','open','close'].indexOf(k) < 0)})
for (var i=0;i<states.length;i++){
var state = states[i]
var tempObj = {}
tempObj[states[i]] = next[states[i]]
tempObj['open'] = end;
end += next[states[i]];
tempObj['close'] = end;
newArr.push(tempObj)
}
} catch (e) {
return false;
}
})
The code will look for all properties other than waterfallData.Popu..,open,close and treats them as states. If you have 10 properties, there will be 7 states excluding the above 3. Then the open and close values of these states are then calculated from the next element and pushed to new array newArr.
Crude.. but works
var newArr = ;
$x = [{
"waterfallData.PopulationName":"Population 1",
"Deaths":-2333,
"Births":8786,
"open":0,
"close":6453
},{
"waterfallData.PopulationName":"Population 2",
"Deaths":-1000,
"Births":5000,
"open":0,
"close":10453
},{
"waterfallData.PopulationName":"Population 3",
"Deaths":-2000,
"Births":500,
"open":0,
"close":8953
}];
$x.forEach((p,i)=>{
var current = $x[i]
newArr.push(current)
try {
var next = $x[i+1];
var start = current.open;
var end = current.close;
var states = Object.keys(current).sort().filter((k)=>{return (['waterfallData.PopulationName','open','close'].indexOf(k) < 0)})
for (var i=0;i<states.length;i++){
var state = states[i]
var tempObj = {}
tempObj[states[i]] = next[states[i]]
tempObj['open'] = end;
end += next[states[i]];
tempObj['close'] = end;
newArr.push(tempObj)
}
} catch (e) {
return false;
}
})
The code will look for all properties other than waterfallData.Popu..,open,close and treats them as states. If you have 10 properties, there will be 7 states excluding the above 3. Then the open and close values of these states are then calculated from the next element and pushed to new array newArr.
edited Nov 9 at 10:08
answered Nov 9 at 8:53
HymnZ
1,3691918
1,3691918
what is $x? It says ReferenceError $x is not defined.
– Syed Ariff
Nov 9 at 9:05
@SyedAriff .. a line got missed.. edited and updated.
– HymnZ
Nov 9 at 9:13
Now try to run your code where you switch the order ofDeaths
andBirths
in the$x
data:$x = [{... "Births":8786, "Deaths":-2333
The output will differ
– HMR
Nov 9 at 9:21
The point is that the output of your code relies on Object.keys (look at the first line) to return the keys in a specific order but this is not the case. The specification says that there is no guaranteed order of the keys.
– HMR
Nov 9 at 9:33
@HMR.. obviously, because by switching order of births & deaths, your representation itself changes.. to make it standardized.. I've added a sort to ensure consistency in the order. On a side note, where can I learn to code minimally like you. :) It's glorious.. :D
– HymnZ
Nov 9 at 10:07
|
show 2 more comments
what is $x? It says ReferenceError $x is not defined.
– Syed Ariff
Nov 9 at 9:05
@SyedAriff .. a line got missed.. edited and updated.
– HymnZ
Nov 9 at 9:13
Now try to run your code where you switch the order ofDeaths
andBirths
in the$x
data:$x = [{... "Births":8786, "Deaths":-2333
The output will differ
– HMR
Nov 9 at 9:21
The point is that the output of your code relies on Object.keys (look at the first line) to return the keys in a specific order but this is not the case. The specification says that there is no guaranteed order of the keys.
– HMR
Nov 9 at 9:33
@HMR.. obviously, because by switching order of births & deaths, your representation itself changes.. to make it standardized.. I've added a sort to ensure consistency in the order. On a side note, where can I learn to code minimally like you. :) It's glorious.. :D
– HymnZ
Nov 9 at 10:07
what is $x? It says ReferenceError $x is not defined.
– Syed Ariff
Nov 9 at 9:05
what is $x? It says ReferenceError $x is not defined.
– Syed Ariff
Nov 9 at 9:05
@SyedAriff .. a line got missed.. edited and updated.
– HymnZ
Nov 9 at 9:13
@SyedAriff .. a line got missed.. edited and updated.
– HymnZ
Nov 9 at 9:13
Now try to run your code where you switch the order of
Deaths
and Births
in the $x
data: $x = [{... "Births":8786, "Deaths":-2333
The output will differ– HMR
Nov 9 at 9:21
Now try to run your code where you switch the order of
Deaths
and Births
in the $x
data: $x = [{... "Births":8786, "Deaths":-2333
The output will differ– HMR
Nov 9 at 9:21
The point is that the output of your code relies on Object.keys (look at the first line) to return the keys in a specific order but this is not the case. The specification says that there is no guaranteed order of the keys.
– HMR
Nov 9 at 9:33
The point is that the output of your code relies on Object.keys (look at the first line) to return the keys in a specific order but this is not the case. The specification says that there is no guaranteed order of the keys.
– HMR
Nov 9 at 9:33
@HMR.. obviously, because by switching order of births & deaths, your representation itself changes.. to make it standardized.. I've added a sort to ensure consistency in the order. On a side note, where can I learn to code minimally like you. :) It's glorious.. :D
– HymnZ
Nov 9 at 10:07
@HMR.. obviously, because by switching order of births & deaths, your representation itself changes.. to make it standardized.. I've added a sort to ensure consistency in the order. On a side note, where can I learn to code minimally like you. :) It's glorious.. :D
– HymnZ
Nov 9 at 10:07
|
show 2 more comments
up vote
0
down vote
I'll give it a try, you can specify what keys you need to be extra data but the original data must have values for those keys. It'll create the extra items in the order that the keys are provided:
const data = [{"waterfallData.PopulationName":"Population 1","Deaths":-2333,"Births":8786,"open":0,"close":6453},{"waterfallData.PopulationName":"Population 2","Deaths":-1000,"Births":5000,"open":0,"close":10453},{"waterfallData.PopulationName":"Population 3","Deaths":-2000,"Births":500,"open":0,"close":8953}];
const zip = (arr1, arr2) =>
[...new Array(Math.max(arr1.length, arr2.length))]
.map((_, i) => i)
.map((i) => [arr1[i], arr2[i]]);
const extras = (keys) => ([current, next]) =>
keys.map((key) => [key, next[key]]).reduce(
([result, lastClose], [key, nextValue]) => [
result.concat({
[key]: nextValue,
open: lastClose,
close: nextValue + lastClose,
}),
nextValue + lastClose,
],
[, current.close],
)[0];
const withExtras = (keys) => ([current, next]) =>
!next
? [current]
: [current].concat(extras(keys)([current, next]));
console.log(
zip(data, data.slice(1)) //having [[current,next],[current,next]...]
.map(withExtras(['Deaths', 'Births']))
.flatten(),
);
console.log(
'diffenrent order different result',
zip(data, data.slice(1))
.map(withExtras(['Births', 'Deaths']))
.flatten(),
);
add a comment |
up vote
0
down vote
I'll give it a try, you can specify what keys you need to be extra data but the original data must have values for those keys. It'll create the extra items in the order that the keys are provided:
const data = [{"waterfallData.PopulationName":"Population 1","Deaths":-2333,"Births":8786,"open":0,"close":6453},{"waterfallData.PopulationName":"Population 2","Deaths":-1000,"Births":5000,"open":0,"close":10453},{"waterfallData.PopulationName":"Population 3","Deaths":-2000,"Births":500,"open":0,"close":8953}];
const zip = (arr1, arr2) =>
[...new Array(Math.max(arr1.length, arr2.length))]
.map((_, i) => i)
.map((i) => [arr1[i], arr2[i]]);
const extras = (keys) => ([current, next]) =>
keys.map((key) => [key, next[key]]).reduce(
([result, lastClose], [key, nextValue]) => [
result.concat({
[key]: nextValue,
open: lastClose,
close: nextValue + lastClose,
}),
nextValue + lastClose,
],
[, current.close],
)[0];
const withExtras = (keys) => ([current, next]) =>
!next
? [current]
: [current].concat(extras(keys)([current, next]));
console.log(
zip(data, data.slice(1)) //having [[current,next],[current,next]...]
.map(withExtras(['Deaths', 'Births']))
.flatten(),
);
console.log(
'diffenrent order different result',
zip(data, data.slice(1))
.map(withExtras(['Births', 'Deaths']))
.flatten(),
);
add a comment |
up vote
0
down vote
up vote
0
down vote
I'll give it a try, you can specify what keys you need to be extra data but the original data must have values for those keys. It'll create the extra items in the order that the keys are provided:
const data = [{"waterfallData.PopulationName":"Population 1","Deaths":-2333,"Births":8786,"open":0,"close":6453},{"waterfallData.PopulationName":"Population 2","Deaths":-1000,"Births":5000,"open":0,"close":10453},{"waterfallData.PopulationName":"Population 3","Deaths":-2000,"Births":500,"open":0,"close":8953}];
const zip = (arr1, arr2) =>
[...new Array(Math.max(arr1.length, arr2.length))]
.map((_, i) => i)
.map((i) => [arr1[i], arr2[i]]);
const extras = (keys) => ([current, next]) =>
keys.map((key) => [key, next[key]]).reduce(
([result, lastClose], [key, nextValue]) => [
result.concat({
[key]: nextValue,
open: lastClose,
close: nextValue + lastClose,
}),
nextValue + lastClose,
],
[, current.close],
)[0];
const withExtras = (keys) => ([current, next]) =>
!next
? [current]
: [current].concat(extras(keys)([current, next]));
console.log(
zip(data, data.slice(1)) //having [[current,next],[current,next]...]
.map(withExtras(['Deaths', 'Births']))
.flatten(),
);
console.log(
'diffenrent order different result',
zip(data, data.slice(1))
.map(withExtras(['Births', 'Deaths']))
.flatten(),
);
I'll give it a try, you can specify what keys you need to be extra data but the original data must have values for those keys. It'll create the extra items in the order that the keys are provided:
const data = [{"waterfallData.PopulationName":"Population 1","Deaths":-2333,"Births":8786,"open":0,"close":6453},{"waterfallData.PopulationName":"Population 2","Deaths":-1000,"Births":5000,"open":0,"close":10453},{"waterfallData.PopulationName":"Population 3","Deaths":-2000,"Births":500,"open":0,"close":8953}];
const zip = (arr1, arr2) =>
[...new Array(Math.max(arr1.length, arr2.length))]
.map((_, i) => i)
.map((i) => [arr1[i], arr2[i]]);
const extras = (keys) => ([current, next]) =>
keys.map((key) => [key, next[key]]).reduce(
([result, lastClose], [key, nextValue]) => [
result.concat({
[key]: nextValue,
open: lastClose,
close: nextValue + lastClose,
}),
nextValue + lastClose,
],
[, current.close],
)[0];
const withExtras = (keys) => ([current, next]) =>
!next
? [current]
: [current].concat(extras(keys)([current, next]));
console.log(
zip(data, data.slice(1)) //having [[current,next],[current,next]...]
.map(withExtras(['Deaths', 'Births']))
.flatten(),
);
console.log(
'diffenrent order different result',
zip(data, data.slice(1))
.map(withExtras(['Births', 'Deaths']))
.flatten(),
);
const data = [{"waterfallData.PopulationName":"Population 1","Deaths":-2333,"Births":8786,"open":0,"close":6453},{"waterfallData.PopulationName":"Population 2","Deaths":-1000,"Births":5000,"open":0,"close":10453},{"waterfallData.PopulationName":"Population 3","Deaths":-2000,"Births":500,"open":0,"close":8953}];
const zip = (arr1, arr2) =>
[...new Array(Math.max(arr1.length, arr2.length))]
.map((_, i) => i)
.map((i) => [arr1[i], arr2[i]]);
const extras = (keys) => ([current, next]) =>
keys.map((key) => [key, next[key]]).reduce(
([result, lastClose], [key, nextValue]) => [
result.concat({
[key]: nextValue,
open: lastClose,
close: nextValue + lastClose,
}),
nextValue + lastClose,
],
[, current.close],
)[0];
const withExtras = (keys) => ([current, next]) =>
!next
? [current]
: [current].concat(extras(keys)([current, next]));
console.log(
zip(data, data.slice(1)) //having [[current,next],[current,next]...]
.map(withExtras(['Deaths', 'Births']))
.flatten(),
);
console.log(
'diffenrent order different result',
zip(data, data.slice(1))
.map(withExtras(['Births', 'Deaths']))
.flatten(),
);
const data = [{"waterfallData.PopulationName":"Population 1","Deaths":-2333,"Births":8786,"open":0,"close":6453},{"waterfallData.PopulationName":"Population 2","Deaths":-1000,"Births":5000,"open":0,"close":10453},{"waterfallData.PopulationName":"Population 3","Deaths":-2000,"Births":500,"open":0,"close":8953}];
const zip = (arr1, arr2) =>
[...new Array(Math.max(arr1.length, arr2.length))]
.map((_, i) => i)
.map((i) => [arr1[i], arr2[i]]);
const extras = (keys) => ([current, next]) =>
keys.map((key) => [key, next[key]]).reduce(
([result, lastClose], [key, nextValue]) => [
result.concat({
[key]: nextValue,
open: lastClose,
close: nextValue + lastClose,
}),
nextValue + lastClose,
],
[, current.close],
)[0];
const withExtras = (keys) => ([current, next]) =>
!next
? [current]
: [current].concat(extras(keys)([current, next]));
console.log(
zip(data, data.slice(1)) //having [[current,next],[current,next]...]
.map(withExtras(['Deaths', 'Births']))
.flatten(),
);
console.log(
'diffenrent order different result',
zip(data, data.slice(1))
.map(withExtras(['Births', 'Deaths']))
.flatten(),
);
edited Nov 9 at 10:08
answered Nov 9 at 9:12
HMR
13.2k103898
13.2k103898
add a comment |
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%2f53221806%2fhow-to-append-an-object-to-an-array-based-on-the-number-of-properties-other-than%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
Why not create using nested JSON?
– HymnZ
Nov 9 at 8:07
@HymnZ because I am using Amcharts and it does not support nested JSON data.
– Syed Ariff
Nov 9 at 8:09
You can try with Object.keys(your_array[1]) to create extra properties.. Just as @Grégory NEUT answered
– HymnZ
Nov 9 at 8:14
1
NB2: object properties are unordered, so the concept of "the number of properties after..." is unreliable. All you could say is "the number of properties other than...".
– trincot
Nov 9 at 8:21
1
Hey, no cheating. You changed the question from using properties such as 'State [Number]' to 'deatsh' and 'biths'. Trincot's deleted answer had a comment where he states that Object.keys is not to be guaranteed to come in any particular order so how will you know what's next and previous?
– HMR
Nov 9 at 8:43