0

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.

1
  • You can loop an objects values with a " for in" and you can find a key with "hasownproperty". Commented May 14, 2020 at 14:59

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

7 Comments

This looks really clean! Thank you, I like it.
Thank you and I'm glad that it helped. :-)
I'm new to javaScript but I wonder why it's not possible to simply to this const resultArray = Object.values(test.resources).flatMap((r) => r.resourceScope).filter((r)=> r)
If we don't use Set, then the data might get repeated which is not intended. Otherwise again we'll have to filter from the resultant array. In order to avoid that used Set which will hold unique values.
I see now, when we use new Set() it creates a Set object and not an object of type array. I was a bit confused there I think. Thanks again for the great help!
|
2

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)

Comments

0

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

Comments

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.