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 eg lodash
functions as well but all in vain.
Thanks
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) ,[] ) ;
为什么不直接使用 lodash flatten https://lodash.com/docs/4.17.4#flatten
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);
In case you want to play with more ES6 features:
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, ...o], []); console.log(result);
Use Lodash Library _.flattenDeep
let allCheckboxFiltered = [
[{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"}]
];
allCheckboxFiltered = _.flattenDeep(allCheckboxFiltered);
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);
只需使用下面的 Lodash 函数
_.flattenDeep(temp1);
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.