I have working array filter using javascript and lodash. I want to converts array of array into a single array. Anyone can you tell converts in single array.
Input data
const inputData = [
[
{ 'Tempe(C)': 12 },
{ 'Tempe(C)': 13 },
{ 'Tempe(C)': 14 },
{ 'Tempe(C)': 15 }
],
[
{ 'Tempe(C)': 22 },
{ 'Tempe(C)': 23 },
{ 'Tempe(C)': 24 },
{ 'Tempe(C)': 25 }
],
[
{ 'Tempe(C)': 32 },
{ 'Tempe(C)': 33 },
{ 'Tempe(C)': 34 },
{ 'Tempe(C)': 35 }
],
];
I want this structure of output
const outputData = [
[12, 13, 14, 15],
[22, 23, 24, 25],
[32, 33, 34, 35],
];
You could use Object.values
and map
const inputData = [[{"Tempe(C)":12},{"Tempe(C)":13},{"Tempe(C)":14},{"Tempe(C)":15}],[{"Tempe(C)":22},{"Tempe(C)":23},{"Tempe(C)":24},{"Tempe(C)":25}],[{"Tempe(C)":32},{"Tempe(C)":33},{"Tempe(C)":34},{"Tempe(C)":35}]]; const output = inputData.map(arr => arr.map(a => Object.values(a)[0])) console.log(output)
If flat
is supported in your browser:
const inputData = [[{"Tempe(C)":12},{"Tempe(C)":13},{"Tempe(C)":14},{"Tempe(C)":15}],[{"Tempe(C)":22},{"Tempe(C)":23},{"Tempe(C)":24},{"Tempe(C)":25}],[{"Tempe(C)":32},{"Tempe(C)":33},{"Tempe(C)":34},{"Tempe(C)":35}]] const output = inputData.map(arr => arr.map(Object.values).flat()) console.log(output)
(This works for any key. If the key is always Tempe(C)
, @eol's answer is a much better option)
If Tempe(C)
is always the key for the values, you can simply do:
const inputData=[[{"Tempe(C)":12},{"Tempe(C)":13},{"Tempe(C)":14},{"Tempe(C)":15}],[{"Tempe(C)":22},{"Tempe(C)":23},{"Tempe(C)":24},{"Tempe(C)":25}],[{"Tempe(C)":32},{"Tempe(C)":33},{"Tempe(C)":34},{"Tempe(C)":35}]]; const output = inputData.map(arr => arr.map(data => data['Tempe(C)'])); console.log(output)
Hi Ragu here is how you can achieve the wanted result :
var inputData = [
[
{ 'Tempe(C)': 12 },
{ 'Tempe(C)': 13 },
{ 'Tempe(C)': 14 },
{ 'Tempe(C)': 15 }
],
[
{ 'Tempe(C)': 22 },
{ 'Tempe(C)': 23 },
{ 'Tempe(C)': 24 },
{ 'Tempe(C)': 25 }
],
[
{ 'Tempe(C)': 32 },
{ 'Tempe(C)': 33 },
{ 'Tempe(C)': 34 },
{ 'Tempe(C)': 35 }
],
];
var newstructure = [];
for(var i in inputData){
newstructure[i] = [];
for(var j in inputData[i]){
newstructure[i][j] = inputData[i][j]["Tempe(C)"];
}
}
With lodash you can use nested _.map()
calls to get the result:
const data = [[{"Tempe(C)":12},{"Tempe(C)":13},{"Tempe(C)":14},{"Tempe(C)":15}],[{"Tempe(C)":22},{"Tempe(C)":23},{"Tempe(C)":24},{"Tempe(C)":25}],[{"Tempe(C)":32},{"Tempe(C)":33},{"Tempe(C)":34},{"Tempe(C)":35}]] const result = _.map(data, o => _.map(o, 'Tempe(C)')) console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>
And the same idea using lodash/fp :
const fn = _.map(_.map('Tempe(C)')) const data = [[{"Tempe(C)":12},{"Tempe(C)":13},{"Tempe(C)":14},{"Tempe(C)":15}],[{"Tempe(C)":22},{"Tempe(C)":23},{"Tempe(C)":24},{"Tempe(C)":25}],[{"Tempe(C)":32},{"Tempe(C)":33},{"Tempe(C)":34},{"Tempe(C)":35}]] const result = fn(data) console.log(result)
<script src='https://cdn.jsdelivr.net/g/lodash@4(lodash.min.js+lodash.fp.min.js)'></script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.