JavaScript convert array of array of objects into single array of objects in lodash convert array to array list,convert array to array of

I have an array of arrays of objects as below which is generated by _.filter lodash function which returns objects for the matched values:

const matchedPositions = _.filter(allPositions, function(o) {
return o.company.toLowerCase() === value.toLowerCase();
});//matchedPositions have array of objects
let allCheckboxFiltered = [...allCheckboxFiltered, matchedPositions]; //allCheckboxFiltered  have below results

   [
    [{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
    {serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}],
    [{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
    {serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
    {serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}]
    ];

I wanted as below intended result;

[
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}
];

I have tried reduce function to generate such intended result as below:

let result = temp1.reduce(function(res, obj) {
console.log(res, obj);
Object.keys(obj).forEach(function(k) {
res[k] = res[k] || [];
res[k] = res[k].concat(obj[k]);
});
return res;
}, []);

Above code is not generating the result, I wanted. I have tried other stuffs e.g. lodash functions as well but all in vain.

Thanks

Answer:1

You can use reduce like below:

var temp1 = [
[{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}],
[{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"},
{serialNo: 9, sector: null, company: "AMERICAN LIFE INSURANCE COMPANY", location: null, position: "INSURANCE Operations  MANAGER"}]
];

var result= temp1.reduce((o, m) => m.concat(o), []);
console.log(result);
Answer:2

You can use reduce

var output = temp1.reduce( (a,b) => (a = a.concat(b), a) ,[] ) ;

Or simply

var output = temp1.reduce( (a,b) => a.concat(b) ,[] ) ;
Answer:3

Why dont you just use lodash flatten https://lodash.com/docs/4.17.4#flatten

Answer:4

You can use Array.concat in order to spread the inner array.

var temp1 = [
  [{
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    },
    {
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    }
  ],
  [{
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    },
    {
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    },
    {
      serialNo: 9,
      sector: null,
      company: "AMERICAN LIFE INSURANCE COMPANY",
      location: null,
      position: "INSURANCE Operations  MANAGER"
    }
  ]
];

const result = temp1.reduce((res, currentArr) => res.concat(currentArr), [])
console.log(result);
Answer:5

Just use below Lodash function

_.flattenDeep(temp1);
Answer:6

I'm trying to solve the famous FizzBuzz quiz but I decided to use the logical operator or instead of else to provide fullback. for (var num = 1; num <= 100; num++) { var output; if (num %...

I'm trying to solve the famous FizzBuzz quiz but I decided to use the logical operator or instead of else to provide fullback. for (var num = 1; num <= 100; num++) { var output; if (num %...

  1. logical operators with dates in excel
  2. logical operators with example
  3. logical operator with select statement
  4. logical operators with truth table
  5. logical operators with integers
  6. logical operators with symbols
  7. logical operators with example in java
  8. logical operator with null
  9. logical operator with example program
  10. logical operator with
  11. logical operator with definition
  12. logical operator with function
  13. logical operators in c
  14. logical operator in python
  15. logical operator in java
  16. logical operator in r
  17. logical operator in javascript
  18. logical operator in sql
  19. logical operator in php
  20. logical operator in matlab

I need to do, when user enter number in input box and click on button to get data, then it call a java method which returns the Array list of records,now in jsp page data need to be store in table ...

I need to do, when user enter number in input box and click on button to get data, then it call a java method which returns the Array list of records,now in jsp page data need to be store in table ...

  1. using jquery plugin in angular
  2. using jquery plugin in react
  3. use jquery plugin
  4. use jquery plugin vue
  5. jquery from plugin
  6. use jquery validation plugin
  7. use jquery mask plugin
  8. use jquery wordpress plugin
  9. use jquery migrate plugin
  10. form validation using jquery plugin
  11. form validation using jquery plugin examples

Have a simple div array with values that the user will choose from and an "other" field where they can put in their own number. I have a clearing div (lower right) that will clear the selection and ...

Have a simple div array with values that the user will choose from and an "other" field where they can put in their own number. I have a clearing div (lower right) that will clear the selection and ...

  1. javascript stoppropagation not working
  2. javascript stoppropagation not working in firefox
  3. javascript stoppropagation doesn't work
  4. javascript event.stoppropagation not working

How do you modify the function below to make the second parameter optional? TypeScript: function getName(name: string, { lastName }: { lastName: string }) { // ... } getName('John'); /...

How do you modify the function below to make the second parameter optional? TypeScript: function getName(name: string, { lastName }: { lastName: string }) { // ... } getName('John'); /...