I had JSON data that came back with single int values. With some changes, the values are now coming back as arrays of ints (as well as the original format).
{
"value": 10,
"value": 70,
"value": 30,
"value": 200
}
- and -
{
"value": [64, 13, 55, 34, 52, 43, 59, 20, 20],
"value": [10, 90, 20, 80, 30, 70, 60, 40, 50]
}
I had a formula that would return the min, max, and sum of the old version of JSON data. Now it doesn't work, and I can't figure out what would be the best way to re-write the function to handle the arrays. Or if its better to make a second function to handle just arrays and do a check if it is an int or array?
Is there a way that would return (from the numbers above):
// no value array, apply to all
[ 10, 200, 310 ] // min, max, sum
- and -
// for each of the value arrays
[ 23, 64, 360 ] // val 1 - min, max, sum
[ 10, 90, 450 ] // val 2 - min, max, sum
// input data
const value = document.querySelectorAll( "div" ).forEach( el => {
const contents = el.textContent, // get the text in the <div>
json = JSON.parse( contents ), // parse the data
jsonData = json.data; // get the data only
// normalise the data
// #from: https://stackoverflow.com/a/67294607/1086990
const normaliseData = arr => {
const data = arr.map(({ value }) => value);
return typeof arr[0].value === 'number' ? [data] : data;
};
// add into const
const valueArray = normaliseData( jsonData );
// get the min / max / sum
const minMaxSum = valueArray.forEach( e => {
return [
Math.min(...e),
Math.max(...e),
[...e].reduce((v, w) => v + w)
];
});
// output
console.log( minMaxSum );
});
<div>
{ "data": [ { "value": [64, 23, 45, 34, 52, 43, 59, 40] }, { "value": [10, 90, 20, 80, 30, 70, 60, 40, 50] } ] }
</div>
<div>
{ "data": [ { "value": 600 }, { "value": 70 }, { "value": 30 } ] }
</div>
Normalize the data by testing the type of the value of the first object in each array:
const valueInArray = [{ value: [64, 23] }, { value: [45, 34] }];
const valueAsSingle = [{ value: 600 }, { value: 70 }];
const normalizeData = arr => {
const data = arr.map(({ value }) => value);
return typeof arr[0].value === 'number'
? [data]
: data;
};
console.log(normalizeData(valueInArray));
//=> [ [ 64, 23 ], [ 45, 34 ] ]
console.log(normalizeData(valueAsSingle));
//=> [ [ 600, 70 ] ]
Now they are the same shape, and so you can treat them equally.
You can use Math.max and Math.min to find the maximum and minimum of the array then assign the values in the specific variables.
Currently, when you are using val.value it is consisting of the whole array and hence you also need to iterate over the array to find the max, min, or sum.
To find the sum use reduce on the val.value array and then add it in the acc[2].
// input data
const valueInArray = document.getElementById("valueInArray").innerHTML,
valueAsSingle = document.getElementById("valueAsSingle").innerHTML;
// parse
const jsonArray = JSON.parse( valueInArray ),
jsonNumber = JSON.parse( valueAsSingle ),
jsonArrayData = jsonArray.data,
jsonNumberData = jsonNumber.data;
// get numbers
const minMaxSumArray = jsonArrayData.reduce( ( acc, val ) => {
// smallest number
acc[0] = (
( acc[0] === undefined || Math.min(...val.value) < acc[0] ) ?
Math.min(...val.value) : acc[0]
)
// largest number
acc[1] = (
( acc[1] === undefined || Math.max(...val.value) > acc[1] ) ?
Math.max(...val.value) : acc[1]
)
// sum of numbers
acc[2] = (
acc[2] === undefined ?
val.value.reduce((v, w) => v + w) : val.value.reduce((v, w) => v + w) + acc[2]
)
console.log('answer', acc)
// return the array
return acc;
}, [] );
<div id="valueInArray">
{ "data": [ { "value": [64, 23, 45, 34, 52, 43, 59, 40] }, { "value": [10, 90, 20, 80, 30, 70, 60, 40, 50] } ] }
</div>
<div id="valueAsSingle">
{ "data": [ { "value": 10 }, { "value": 70 }, { "value": 30 } ] }
</div>
My take on it: first, create a single Array of all values (either arrays or single) by concatening them and using Array.flat() to flatten it. Then use a reducer to determine the sum and use Math.min/max for the min and max values.
// input data
const valuesInArray = JSON.parse(
document.querySelector("#valueInArray").textContent).data;
const singleValues = JSON.parse(
document.querySelector("#valueAsSingle").textContent).data;
// get all values from the objects to a single Array of values
// (so: convert all to single values)
const allValues = valuesInArray.map( v => v.value )
.concat(singleValues.reduce( (acc, val) => [...acc, +val.value], [] ) )
.flat();
// let's see what we have
console.log(`All values from both objects: ${JSON.stringify(allValues)}`);
// create sum, min and max
const [ sum, min, max, ] = [
allValues.reduce( (a, v) => a + +v, 0),
Math.min(...allValues),
Math.max(...allValues) ];
console.log(`From all values sum is ${sum}, min ${min} and max ${max}`);
div {
display: none;
}
<div id="valueInArray">
{ "data": [
{ "value": [64, 23, 45, 34, 52, 43, 59, 40] },
{ "value": [10, 90, 20, 80, 30, 70, 60, 40, 50] } ]
}
</div>
<div id="valueAsSingle">
{ "data":
[ { "value": 10 }, { "value": 70 }, { "value": 30 } ]
}
</div>
The second snippet aggregates data per value, where the single values are added as a values array to valuesInArray.
// input data
const valuesInArray = JSON.parse(
document.querySelector("#valueInArray").textContent).data;
const singleValues = JSON.parse(
document.querySelector("#valueAsSingle").textContent).data;
// create sum, min and max *per value*, in one go
const aggregatesAdded = valuesInArray
.concat({ value: singleValues.reduce( (acc, val) => [...acc, +val.value], [] ) } )
.reduce( (acc, val) => [...acc, {...val, aggregatedValues: {
sum: val.value.reduce( (a, v) => a + +v, 0 ),
min: Math.min(...val.value),
max: Math.max(...val.value) } }
], [])
document.querySelector("pre").textContent =
JSON.stringify({data: aggregatesAdded}, null, 2);
div {
display: none;
}
<div id="valueInArray">
{ "data": [
{ "value": [64, 23, 45, 34, 52, 43, 59, 40] },
{ "value": [10, 90, 20, 80, 30, 70, 60, 40, 50] } ]
}
</div>
<div id="valueAsSingle">
{ "data":
[ { "value": 10 }, { "value": 70 }, { "value": 30 } ]
}
</div>
<pre id="result"></pre>
Related
Have an array of notes and corresponding time, Im separating ALL the repeating Time entries with code below to a new object called duplicatesTimeValues.
const allNotes = [
{
"note": 69,
"time": 0
},
{
"note": 57,
"time": 0
},
{
"note": 60,
"time": 1.5
},
{
"note": 64,
"time": 2
},
{
"note": 69,
"time": 2.5
},
{
"note": 71,
"time": 3
},
{
"note": 52,
"time": 3
},
{
"note": 64,
"time": 4.5
},
{
"note": 68,
"time": 5
},
{
"note": 71,
"time": 5.5
}
];
const getDuplicates = () => {
const values = allNotes;
const lookup = values.reduce((a, e) => {
a[e.time] = ++a[e.time] || 0;
console.log(e.keys);
return a;
}, {});
console.log('repeating');
const duplicatesTimeValues = values.filter(e => lookup[e.time]);
console.log(duplicates);
const uniqueTimeValues = values.filter(e => !lookup[e.time]);
console.log('unique');
console.log(uniqueValues);
Now I need to compare this output with another array, that might look something like this
[57, 69, 60, 64, 69, 52, 71, 64, 68, 71]
but I need to be able to split that array based on how this object was split.
In order to do that, I would like to get position in original object of the repeating elements.
In this case this entries are
[
{
"note": 69,
"time": 0
},
{
"note": 57,
"time": 0
},
{
"note": 71,
"time": 3
},
{
"note": 52,
"time": 3
}
]
and result I need is
[0, 1, 5, 6]
as repeating elements were on these positions in original allNotes object. And then I will use these positions array to split array I want to compare with, so it looks like this
[60, 64, 69, 64, 68, 71]
How could I do that?
What you want to do is not very clear according to me, but here is a code that gets the indexes of the duplicate elements in an array. Note that it gets the index of a duplicate only if it has been seen before (therefore you'll never get 0 in the output).
const allNotes = [
{
note: 69,
time: 0,
},
{
note: 57,
time: 0,
},
{
note: 60,
time: 1.5,
},
{
note: 64,
time: 2,
},
{
note: 69,
time: 2.5,
},
{
note: 71,
time: 3,
},
{
note: 52,
time: 3,
},
{
note: 64,
time: 4.5,
},
{
note: 68,
time: 5,
},
{
note: 71,
time: 5.5,
},
];
function getIndexesOfDuplicates() {
const indexes = [];
const previousNotes = [];
for (let i = 0; i < allNotes.length; i++) {
let currentNote = allNotes[i].note;
let found = false;
for (let previousNote of previousNotes) {
if (previousNote === currentNote) {
indexes.push(i);
found = true;
break;
}
}
if (!found) previousNotes.push(currentNote);
}
return indexes;
}
console.log(getIndexesOfDuplicates());
Here's a one-liner that makes use of Lodash, specifically the lodash/fp module:
let result =
_.map(
_.last,
_.flatten(
_.filter(
x => x.length > 1,
_.groupBy(
_.compose(_.iteratee('time'), _.first),
_.zip(allNotes, [...Array(allNotes.length).keys()])))))
// result now contains the array [0, 1, 5, 6]
I zip allNotes together with their indices
then I group them if the resulting arrays have the 'time' of their first entry equal
then I retain only the groups longer than 1 element, filtering out the others
then I flatten the resulting array of groups in a single array
and finally get the last element (which is the index appended via _.zip) from each entry
Found solution where I get keys of duplicate items based on values, it isn't cleanest solution, though it will work in my case, as in my use case, values are always unique...
const array = [];
for (var o = 0; o < duplicates.length; o++) {
const value = duplicates[o];
array.push(Object.keys(window.allNotes)[Object.values(window.allNotes).indexOf(value)]);
}
console.log(array);
I have an array:
array = {
"data": [
{ "value": [ 100, 13, 16 ] },
{ "value": [ 101, 14, 17 ] },
{ "value": [ 12, 15, 18 ] }
]
}
Which I am reformatting into a new array of just the columns:
const columnArray = jsonData.map( (current, index, arr) => {
let out = [];
for( let i = 0; i < current.value.length; i++ ) {
out.push( arr[ i ].value[ index ] );
}
return out;
});
// output
[
[ 100, 101, 12 ],
[ 13, 14, 15 ],
[ 16, 17, 18 ]
]
How would I re-write the columnArray mapping to do the column array and be able to sum from the previous value?
So the intended output from the original array would be:
[
[ 100, 201, 213 ],
[ 13, 27, 42 ],
[ 16, 33, 51 ]
]
I would also like the summing to be scalable (though it will always be in a 1:1 ratio). So if the data has 20 items, then each value will have 20 integers in that array too.
I have tried looping through but that didn't work as I only sum from the previous, not all the previous. And this wouldn't scale either:
const columnArray = jsonData.map( (current, index, arr) => {
let out = [];
for( let i = 0; i < current.value.length; i++ ) {
// dont touch first
if( i < 1 ) {
out.push( arr[ i ].value[ index ] );
} else {
out.push( arr[ i ].value[ index ] + arr[ i - 1 ].value[ index ] )
}
}
return out;
});
Instead of pushing the array element, add it to a variable accumulating the running totals, and push that.
const jsonData = [{
"value": [100, 13, 16]
},
{
"value": [101, 14, 17]
},
{
"value": [12, 15, 18]
}
];
const columnArray = jsonData.map((current, index, arr) => {
let out = [];
let total = 0;
for (let i = 0; i < current.value.length; i++) {
total += arr[i].value[index]
out.push(total);
}
return out;
});
console.log(columnArray);
or with a nested map():
const jsonData = [{
"value": [100, 13, 16]
},
{
"value": [101, 14, 17]
},
{
"value": [12, 15, 18]
}
];
const columnArray = jsonData.map((current, index, arr) => {
let total = 0;
return arr.map(el => total += el.value[index])
});
console.log(columnArray);
You're thinking this in the wrong way. You're storing the sum in the list, not anywhere else. So even tho your index is increasing, the resulting sum resides in the list, so to achieve your goal you have to save it in some variable then push the variable into the final list. Follow this code below:
const columnArray = array.data.map((current, index, arr) => {
let out = [];
let temp;
for (let i = 0; i < current.value.length; i++) {
// dont touch first
if (i < 1) {
temp = arr[i].value[index];
out.push(arr[i].value[index]);
} else {
temp = arr[i].value[index] + temp;
out.push(temp);
}
}
return out;
});
something like that...
const array0 = {
"data": [
{ "value": [ 100, 13, 16 ] },
{ "value": [ 101, 14, 17 ] },
{ "value": [ 12, 15, 18 ] }
]
}
const
rowCount = array0.data.reduce((c,{value})=>Math.max(c,value.length) ,0)
, arrResult = Array(rowCount).fill(0).map(x=>Array(array0.data.length).fill(0))
;
arrResult.forEach((_,i,arr)=>
{
array0.data[i].value.forEach((v,j)=>
{
arr[j][i] = v + (i? arr[j][i-1] : 0 )
})
})
console.log( arrResult)
.as-console-wrapper {max-height: 100%!important;top:0}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Hi i am having an angular project .
Currently I am having an array named historyArr . it has some statistics data for 2 days as below.
[
{
"dateRange": "2020-07-01T16:00:00.000+0000",
"total": 20,
"delivered": 5,
"undeliverable": 5,
"expired": 5,
"enroute": 5
},
{
"dateRange": "2020-07-01T17:00:00.000+0000",
"total": 50,
"delivered": 10,
"undeliverable": 15,
"expired": 10,
"enroute": 15
},
{
"dateRange": "2020-07-01T18:00:00.000+0000",
"total": 8,
"delivered": 2,
"undeliverable": 2,
"expired": 2,
"enroute": 2
},
{
"dateRange": "2020-07-02T00:00:00.000+0000",
"total": 160,
"delivered": 40,
"undeliverable": 40,
"expired": 40,
"enroute": 40
},
{
"dateRange": "2020-07-02T01:00:00.000+0000",
"total": 200,
"delivered": 50,
"undeliverable": 50,
"expired": 50,
"enroute": 50
}
]
I want to reduce the above array , so that i would like to perform the summation of statistics per day . So I want to transform the above array to the one below. I am a free to use moment.js libaries for parsing dates.
[
{
"dateRange": "2020-07-01",
"total": 78,
"delivered": 17,
"undeliverable": 22,
"expired": 17,
"enroute": 22
},
{
"dateRange": "2020-07-02",
"total": 360,
"delivered": 90,
"undeliverable": 90,
"expired": 90,
"enroute": 900
}
]
i know its a difficult question. the project is uploaded in stackblitz
https://stackblitz.com/edit/angular-zqmdpy
https://angular-zqmdpy.stackblitz.io
https://stackblitz.com/edit/angular-zqmdpy?embed=1&file=src/app/app.component.html
really appreciate any help
thank you
You may
do String.prototype.slice() to obtain meaningful portion of dateRange and use that as a key
to traverse your source array (e.g. with Array.prototype.reduce())
and build up the Map having grouped/summarized objects as respective value,
then extract those values into array with Map.prototype.values()
assuming all properties, other than dateRange should get summed up, you may use slight shortcut not to hardcode all of them explicitly:
const src = [{"dateRange":"2020-07-01T16:00:00.000+0000","total":20,"delivered":5,"undeliverable":5,"expired":5,"enroute":5},{"dateRange":"2020-07-01T17:00:00.000+0000","total":50,"delivered":10,"undeliverable":15,"expired":10,"enroute":15},{"dateRange":"2020-07-01T18:00:00.000+0000","total":8,"delivered":2,"undeliverable":2,"expired":2,"enroute":2},{"dateRange":"2020-07-02T00:00:00.000+0000","total":160,"delivered":40,"undeliverable":40,"expired":40,"enroute":40},{"dateRange":"2020-07-02T01:00:00.000+0000","total":200,"delivered":50,"undeliverable":50,"expired":50,"enroute":50}],
result = [...src
.reduce((acc, o) => {
const key = o.dateRange.slice(0,10),
group = acc.get(key)
if(group){
const { dateRange, ...rest } = o
Object
.keys(rest)
.forEach(key =>
group[key] = (group[key] || 0) + (o[key] || 0))
} else {
acc.set(key, {...o, dateRange: key})
}
return acc
}, new Map)
.values()
]
console.log(result)
.as-console-wrapper{min-height:100%;}
This is an idea how you should do it, I'm not taking care at all of the Correct format for the date, you should improve it, I created an auxiliar arr that doesnt mutate in order to not change the original array.
ngOnInit() {
var temp = {};
var obj = null;
this.arr = data;
this.historyArr = this.arr['histogramDistributionbyCdrStatuses'];
this.historyArr.forEach(el=>{
el.dateRange=moment(el.dateRange).format('YYYY-MM-DD')
})
let auxArr = JSON.parse(JSON.stringify(this.historyArr));
for(var i=0; i < auxArr.length; i++) {
obj=auxArr[i];
if(!temp[obj.dateRange]) {
temp[obj.dateRange] = obj;
} else {
temp[obj.dateRange].total += obj.total;
}
}
var result = [];
for (var prop in temp)
result.push(temp[prop]);
console.log(result)
}
I iterate over the array and get the date from an entry. I look if this date is in the help-array index. If no than I create a new entry with the properties from the entry and append it to the result. Otherwise I look with this index in my result-array and sum upevery prperty of my element to this entry.
function transformArray( array ) {
let result = [];
let index = [];
array.forEach(obj => {
const PROPERTIES = Object.keys(obj);
PROPERTIES.splice(PROPERTIES.indexOf('dateRange'),1);
let dat = obj.dateRange.substr(0,10);
let resIndex = index.indexOf(dat);
if ( resIndex == -1) {
index.push(dat);
let entry = { dateRange: dat}
PROPERTIES.forEach (prop => entry[prop] = obj[prop] || 0);
result.push(entry);
} else {
let entry = result[resIndex];
PROPERTIES.forEach (prop => entry[prop] = (entry[prop] || 0) + (obj[prop] || 0));
}
});
return result;
}
let historyArr = [
{
"dateRange": "2020-07-01T16:00:00.000+0000",
"total": 20,
"delivered": 5,
"undeliverable": 5,
"expired": 5,
"enroute": 5
},
{
"dateRange": "2020-07-01T17:00:00.000+0000",
"total": 50,
"delivered": 10,
"undeliverable": 15,
"expired": 10,
"enroute": 15
},
{
"dateRange": "2020-07-01T18:00:00.000+0000",
"total": 8,
"delivered": 2,
"undeliverable": 2,
"expired": 2,
"enroute": 2
},
{
"dateRange": "2020-07-02T00:00:00.000+0000",
"total": 160,
"delivered": 40,
"undeliverable": 40,
"expired": 40,
"enroute": 40
},
{
"dateRange": "2020-07-02T01:00:00.000+0000",
"total": 200,
"delivered": 50,
"undeliverable": 50,
"expired": 50,
"enroute": 50
}
];
console.log(transformArray(historyArr));
let say i have 1 multidimensional array and i want to exclude values that not equal in javascript.
here is the example array.
var filter = ["big_number", "odds_number"];
var arrays = {
"first" : {
"big_number" : [50,51,52],
"odds_number" : [39,41,51,53]
},
"second" : {
"big_number" : [61,62,63,64,65,70,72,73],
"odds_number" : [13,15,17,19,61,63,65,73]
}
};
i want to convert that array to be like this.
var new_arrays = {
"first" : [51],
"second" : [61,63,65,73]
};
here is my code
var newArray = {
"first" : [],
"second" : []
};
for (var k in arrays){
if (arrays.hasOwnProperty(k)) {
for(var f=0; f<filter.length; f++) {
newArray[k].push(arrays[k][filter[f]].filter(value => -1 !== arrays[k][filter[f]].indexOf(value))));
}
}
}
console.log(newArray);
actually i could do this code
var newArray = {
"first" : [],
"second" : []
};
for (var k in arrays){
if (arrays.hasOwnProperty(k)) {
newArray[k].push(arrays[k]["big_number"].filter(value => -1 !== arrays[k]["odds_number"].indexOf(value))));
}
}
console.log(newArray);
but i need to convert it through filter variable.
i could not use filter[0] and filter[1], because that values could change dynamically and could be more than 2 values in array.
You could loop through the keys and update the values using filter and includes:
var arrays={"first":{"big_number":[50,51,52],"odds_number":[39,41,51,53]},"second":{"big_number":[61,62,63,64,65,70,72,73],"odds_number":[13,15,17,19,61,63,65,73]}};
for (let key in arrays) {
arrays[key] = arrays[key]["big_number"]
.filter(n => arrays[key]["odds_number"].includes(n));
}
console.log(arrays)
If you don't want to mutate the original object then use Object.entries and reduce:
var arrays={"first":{"big_number":[50,51,52],"odds_number":[39,41,51,53]},"second":{"big_number":[61,62,63,64,65,70,72,73],"odds_number":[13,15,17,19,61,63,65,73]}};
const newObject = Object.entries(arrays).reduce((r, [key, {big_number, odds_number}]) => {
r[key] = big_number.filter(n => odds_number.includes(n));
return r
}, {})
console.log(newObject)
If you have more than 2 array properties, you can do something like this: Get all the arrays using Object.values and then use reduce to run the previous code recursively
var arrays = {
"first": {
"big_number": [50, 51, 52],
"odds_number": [39, 41, 51, 53],
"another_key": [41, 51, 53]
},
"second": {
"big_number": [61, 62, 63, 64, 65, 70, 72, 73],
"odds_number": [13, 15, 17, 19, 61, 63, 65, 73],
"another_key": [63, 65]
}
};
for (let key in arrays) {
arrays[key] = Object.values(arrays[key])
.reduce((a, b) => a.filter(c => b.includes(c)))
}
console.log(arrays)
Here is a little intersection snippet:
function intersect(a,b){
b.slice()
return a.filter(item=>{
if(b.includes(item)){
b.splice(b.indexOf(item),1)
return true
}
})
}
Using that, you can do this easily:
function intersect(a,b){
b.slice()
return a.filter(item=>{
if(b.includes(item)){
b.splice(b.indexOf(item),1)
return true
}
})
}
var filter = ["big_number", "odds_number"];
var output={}
var arrays = {
"first" : {
"big_number" : [50,51,52],
"odds_number" : [39,41,51,53]
},
"second" : {
"big_number" : [61,62,63,64,65,70,72,73],
"odds_number" : [13,15,17,19,61,63,65,73]
}
};
for(x in arrays){
output[x]=arrays[x][filter[0]]
for(let i=1;i<filter.length;i++){
output[x]=intersect(output[x],arrays[x][filter[i]])
}
}
console.log (output)
use Object.entries to get keys and values and then use reduce
var arrays = {
"first" : {
"big_number" : [50,51,52],
"odds_number" : [39,41,51,53]
},
"second" : {
"big_number" : [61,62,63,64,65,70,72,73],
"odds_number" : [13,15,17,19,61,63,65,73]
}
};
const output =Object.entries(arrays).reduce((accu, [key, {big_number}]) => {
if(!accu[key]) accu[key] = [];
big_number.forEach(num => {
if(num%2 !==0)
accu[key].push(num);
})
return accu;
}, {});
console.log(output);
You can get the unique values from both the arrays using Set and then using filter get only the common values.
var arrays = {"first": {"big_number": [50, 51, 52],"odds_number": [39, 41, 51, 53]},"second": {"big_number": [61, 62, 63, 64, 65, 70, 72, 73],"odds_number": [13, 15, 17, 19, 61, 63, 65, 73]}},
result = Object.keys(arrays).reduce((r,k) => {
let setB = new Set(arrays[k]["big_number"]);
r[k] = [...new Set(arrays[k]["odds_number"])].filter(x => setB.has(x));
return r;
},{});
console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }
I have a set of JSON - that i need to iterate over as I essentially need to create a string which contains certain identifiers in it. At the moment I am mapping over the data and then over stimulus list. ( I have the index which from the event as I only need data from that index).
The problem I have is the cell_id is always the same . And I need to create a string.
const key = `cell_Id${cell_id}:step_Id${item.step_id}:study_id${studyId}:stim_id${item.stimulus_id}`
I suspect I need to add things to an object as I am interating over the data - what is the best way to do this?
thanks
Here is the JSON
{
"study_id": 16,
"data": [
{
"cell_id": 23,
"stimulus_list": [
{
"stimulus_id": 96,
"step_id": 62
}
]
},
{
"cell_id": 24,
"stimulus_list": [
{
"stimulus_id": 95,
"step_id": 61
}
]
}
]
}
data
.map(item => item.stimulus_list)
.map(item => item[index]);
You can have a nested map over the data and then flatten the result using reduce.
const obj = {
"study_id": 16,
"data": [
{
"cell_id": 23,
"stimulus_list": [
{
"stimulus_id": 96,
"step_id": 62
}
]
},
{
"cell_id": 24,
"stimulus_list": [
{
"stimulus_id": 95,
"step_id": 61
}
]
}
]
}
const res = obj.data
.map(item => {
return [].concat(item.stimulus_list.map(stimulus => `cell_Id${item.cell_id}:step_Id${stimulus.step_id}:study_id${obj.study_id}:stim_id${stimulus.stimulus_id}`))
}).reduce((acc, item) => {
acc = acc.concat(item)
return acc;
}, [])
console.log(res)