0

I have two array

var arr1 = ["xxx", "yyy"]

var arr2 = [
 {domain="xxx", kwd="a", position=1},
 {domain="yyy", kwd="a", position=2},

 {domain="xxx", kwd="b", position=1},
 {domain="yyy", kwd="b", position=2},

 {domain="yyy", kwd="c", position=2},

 {domain="xxx", kwd="d", position=1}

 ]

I want that when for a domain in arr1 there isn't a kdw in arr2 for that domain, will be push position="n/a". So based of the arrays above the output should be:

var arr3 = [
 {domain="xxx", kwd="a", position=1},
 {domain="yyy", kwd="a", position=2},

 {domain="xxx", kwd="b", position=1},
 {domain="yyy", kwd="b", position=2},

 {domain="xxx", kwd="c", position="n/a"},
 {domain="yyy", kwd="c", position=2},

 {domain="xxx", kwd="d", position=1},
 {domain="yyy", kwd="d", position="n/a"},
    
]

I have no idea on how to achieve that. I have started this piece of code but I don't know if it is relevant or not:

var arr1 = [domain="xxx", domain="yyy" ]
        
var arr2 = [{domain="xxx", kwd="a", position=1},{domain="yyy", kwd="a", position=2}, {domain="xxx", kwd="b", position=1}, {domain="yyy", kwd="b", position=2},{domain="yyy", kwd="c", position=2}, {domain="xxx", kwd="d", position=1}]   
    
    var arr3 [];
        
        var i;
        for (i = 0; i < arr2.domain.length; i++) { 
        if(// logic  ) {
            arr3.push({
            domain: arr2[i].domain,
            kwd: arr2[i].kwd,
            position: arr2[i].position
          }) else {
            arr3.push({
            domain: arr2[i].domain,
            kwd: arr2[i].domain,
            position: "n/a"
          }
        }
   }

}

Thanks in advance for any help. Kind regards.

4
  • one question, how doesn't arr2 and arr3 produce an error? Commented May 6, 2021 at 23:28
  • Are all the domain values actually unique? Commented May 6, 2021 at 23:44
  • @charlietfl Yes. Commented May 7, 2021 at 2:29
  • 2
    Then provide samples with unique values and expected results. Hard to figure it out when you made them all the same. See minimal reproducible example Commented May 7, 2021 at 2:30

1 Answer 1

1
var arr1 = ["xxx", "yyy"];

var arr2 = [
  { domain: "xxx", kwd: "a", position: 1 },
  { domain: "yyy", kwd: "a", position: 2 },

  { domain: "xxx", kwd: "b", position: 1 },
  { domain: "yyy", kwd: "b", position: 2 },

  { domain: "yyy", kwd: "c", position: 2 },

  { domain: "xxx", kwd: "d", position: 1 }
];

const grouped = arr2.reduce((group, entry) => {
  const lookup = group[entry.kwd] || {};
  return {
    ...group,
    [entry.kwd]: {
      ...lookup,
      [entry.domain]: entry
    }
  };
}, {});

const filledIn = Object.entries(grouped).reduce(
  (arr, [key, group]) => [
    ...arr,
    ...arr1.map((domain) =>
      domain in group
        ? group[domain]
        : {
            domain,
            kwd: key,
            position: "n/a"
          }
    )
  ],
  []
);

console.log(filledIn);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your help. I tried to edit a bit your script but I got an issue. If you please could have a look stackoverflow.

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.