शुभ रात्रि। कुछ धर्मार्थ आत्मा जावास्क्रिप्ट वस्तुओं के बारे में एक समस्या के साथ मेरी मदद करने के लिए। बहुत-बहुत धन्यवाद जो कर सकता है...

var arrA = [
    {language:'php', count_access:5},
    {language:'c', count_access:7}
];

var arrB = [
    {language:'php', count_access:0},
    {language:'c', count_access:0},
    {language:'python', count_access:0},
    {language:'ace', count_access:0},
    {language:'electron', count_access:0},
];

var result = [...arrA, ...arrB].reduce((acc, item) => { 
    return item;
 }) ;

arrA.concat(result)

//How would you do for the output to be

/*[
{language:'php', count_access:5},
{language:'c', count_access:7},
{language:'python', count_access:0},
{language:'ace', count_access:0},
{language:'electron', count_access:0},
]*/
0
Taffarel Xavier 29 अक्टूबर 2019, 04:50

1 उत्तर

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

यह काम करेगा, यह मिलान language के लिए गिनती भी जोड़ता है

var arrA = [
    {language:'php', count:5},
    {language:'c', count:7}
];

var arrB = [
    {language:'php', count:0},
    {language:'c', count:0},
    {language:'python', count:0},
    {language:'ace', count:0},
    {language:'electron', count:0},
];

let result = [...arrA, ...arrB].reduce((acc, item) => {
  let found = acc.find(x => x.language=== item.language);
  if (found) {
    found.count += item.count;
  } else {
    acc.push(Object.assign({},item));
  }
  return acc;
}, [])
console.log(result);
2
Taffarel Xavier 29 अक्टूबर 2019, 02:37