0

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],      
];
0

4 Answers 4

4

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)

Sign up to request clarification or add additional context in comments.

3 Comments

i had faced this type of error : TypeError: arr.map(...).flat is not a function .............this way is working well : inputData.map(arr => arr.map(data => data['Tempe(C)']));
@ragu flat is not supported in your browser. Updated with another option
This way also working well : inputData.map(arr => arr.map(a => Object.values(a)[0]))
2

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)

Comments

0

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)"];
    }
}


Comments

0

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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.