मेरे पास नीचे दी गई वस्तुओं के सरणियों की एक सरणी है जो _.filter लॉश फ़ंक्शन द्वारा उत्पन्न होती है जो मिलान किए गए मानों के लिए ऑब्जेक्ट्स लौटाती है:

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

मैं नीचे इच्छित परिणाम के रूप में चाहता था;

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

मैंने नीचे के रूप में इस तरह के इच्छित परिणाम उत्पन्न करने के लिए reduce फ़ंक्शन का प्रयास किया है:

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

उपरोक्त कोड परिणाम उत्पन्न नहीं कर रहा है, मैं चाहता था। मैंने अन्य सामानों की कोशिश की है उदा। lodash भी काम करता है लेकिन सब बेकार।

धन्यवाद

3
Owais 8 पद 2017, 13:32

6 जवाब

सबसे बढ़िया उत्तर

आप नीचे की तरह कम का उपयोग कर सकते हैं:

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);

यदि आप अधिक ES6 सुविधाओं के साथ खेलना चाहते हैं:

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);
2
Faly 8 पद 2017, 13:40

आप reduce का उपयोग कर सकते हैं

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

या केवल

var output = temp1.reduce( (a,b) => a.concat(b) ,[] ) ;
3
gurvinder372 8 पद 2017, 13:34

आप सिर्फ लॉश फ़्लैटन का उपयोग क्यों नहीं करते? https://lodash.com/docs/4.17.4#flatten

3
Balaji V 8 पद 2017, 13:42

आंतरिक सरणी को फैलाने के लिए आप Array.concat का उपयोग कर सकते हैं।

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);
0
felixmosh 8 पद 2017, 13:36

बस लोदाश फ़ंक्शन के नीचे उपयोग करें

_.flattenDeep(temp1);
0
Kaushik Andani 8 पद 2017, 13:48

Lodash लाइब्रेरी _.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);
1
Renish Gotecha 1 जिंदा 2020, 09:48