DarkLite1
DarkLite1

Reputation: 14705

How to combine multiple arrays and a string into one array?

Consider the following object:

const test = {
  resources: {
    gatewayApi: {
      resourceUri: 'https://bla',
      resourceScope: 'D',
    },
    msGraphProfile: {
      resourceUri: 'https://foo',
      resourceScope: ['A', 'B', 'C'],
    },
    msGraphMail: {
      resourceUri: 'https://bar',
      resourceScope: ['A'],
    },
    msGraphGroupMember: {
      resourceUri: 'https://bob',
    },
  },
}

The goal is to achieve a single array containing only the unique values defined in resourceScope:

const result = ['A', 'B', 'C', 'D']

To get to the desired result I took these steps:

// first create a new array with key value pairs
const newArray = Object.entries(test.resources)

// return only those values that have the property `resourceScope`
const result = newArray.filter(([, val]) => {
  if ('resourceScope' in val) { return val.resourceScope }
})

At this point I'm a bit stuck. I would need to combine all returned arrays and a single string ('A') into one clean array. I've tried looking for examples but I couldn't really figure it out.

Thank you for your help.

Upvotes: 0

Views: 76

Answers (3)

Gazowski
Gazowski

Reputation: 66

Is it correct for you ?

const test = {
  resources: {
    gatewayApi: {
      resourceUri: 'https://bla',
      resourceScope: 'D',
    },
    msGraphProfile: {
      resourceUri: 'https://foo',
      resourceScope: ['A', 'B', 'C'],
    },
    msGraphMail: {
      resourceUri: 'https://bar',
      resourceScope: ['A'],
    },
    msGraphGroupMember: {
      resourceUri: 'https://bob',
    },
  },
}
let tab = []
for(let [key,value] of Object.entries(test.resources)){
  for(let v in value.resourceScope){
    if(!tab.includes(value.resourceScope[v])){
      tab.push(value.resourceScope[v])
    }
  }
  console.log(tab);
}

Upvotes: 0

Nithish
Nithish

Reputation: 6009

By using flatMap, filter and Set I could able to achieve this. Hope this will be helpful

const test = {"resources":{"gatewayApi":{"resourceUri":"https://bla","resourceScope":"D"},"msGraphProfile":{"resourceUri":"https://foo","resourceScope":["A","B","C"]},"msGraphMail":{"resourceUri":"https://bar","resourceScope":["A"]},"msGraphGroupMember":{"resourceUri":"https://bob"}}}

const resultSet = new Set(Object.values(test.resources).flatMap(resource => resource.resourceScope).filter(resourceScope => resourceScope))

const resultArray = Array.from(resultSet)
console.log(resultArray)

Upvotes: 2

Nenad Vracar
Nenad Vracar

Reputation: 122047

You could use reduce method with Set as accumulator value to get unique values and then create an array from that set.

const test = {"resources":{"gatewayApi":{"resourceUri":"https://bla","resourceScope":"D"},"msGraphProfile":{"resourceUri":"https://foo","resourceScope":["A","B","C"]},"msGraphMail":{"resourceUri":"https://bar","resourceScope":["A"]},"msGraphGroupMember":{"resourceUri":"https://bob"}}}

const uniq = Object
  .values(test.resources)
  .reduce((r, e) => {
    [].concat(e.resourceScope)
      .forEach(e => {
        if (e) r.add(e)
      })

    return r;
  }, new Set)


const result = Array.from(uniq)
console.log(result)

Upvotes: 2

Related Questions