0

I am trying to access an object within an object using the key from another object.

I have two objects:

const OutputReference = {Key1: "Some Random String",Key2: "Some Random String"}
const masterKey = {
...
'Key1':{
Label: "Key 1",
view: [1,2,3],
},
'Key2':{
Label: "Key 2",
view: [4,5,6],
},
...
}

OutputReference contains multiple keys and values, and I want match these keys to the keys in masterKey to grab each corresponding 'view'. So far, I use this function to break out OutputReference into a key (k) and value (v):

Object.keys(OutpufReference).filter(([k,v])=>{
...
//code here
...
});

I then want to grab "view" for the corresponding key and store it in an array. I used:

var tempArr = []
tempArr.push(masterKey.k.view)

Making the entire function:

Object.keys(OutpufReference).filter(([k,v])=>{
...
var tempArr = []
tempArr.push(masterKey.k.view)
...
});

The issue is masterKey.k is coming back undefined. Note console.log(k) in this case outputs exactly this: Key1

What I have tried (just to access k):

tempArr.push(masterKey.k)
tempArr.push(masterKey[k])
var temp = JSON.stringify(k)
tempArr.push(masterKey[temp])
Object.keys(masterKey).forEach((v,i)=>{
  if(v === k) //k is the index from mapping OutputReference
    tempArr.push(masterKey.v)
})

None of these work; all return an undefined object (masterKey.v, masterKey[temp], etc.). Note, when doing console.log() on each of these keys (temp, v, k), it outputs the string Key1. However, using

tempArr.push(masterKey.Key1)

Places the correct value in tempArr (Being the object Key1). This is not ideal however, as there are many keys and values in masterKey and OutputReference only contains a few of them.

Where I looked

I researched mozilla's guide on objects, which led me to my previous attempts Mozilla. I also researched this thread Deleting a property from a javascript object. However it recommends what I have already tried.

I see from this thread that JavaScript objects can only use strings as keys, so why doesn't stringifying my key in

var temp = JSON.stringify(k)
tempArr.push(masterKey[temp])

work?

The final output desired: The array tempArr containing every view that outputReference matched with masterKey (In this case: tempArr = [[1,2,3],[4,5,6])

JoshA328
  • 95
  • 6
  • you definitely want `masterkey[k]` instead of `masterkey.k` - see [here](The Object.keys() method returns an array of a given object's own enumerable string-keyed property names.). Please expand on what you mean when you say this "doesn't work" (which you only mention in the context of trying this alongside a bunch of obviously incorrect things). – Robin Zigmond Nov 30 '22 at 22:05
  • I made an edit to the post to clarify; using masterKey[k] returns an undefined object. Ideally, I would like to use every key from OutputReference, match this with the keys inside masterKey, and return the 'view' object from each of these matches. – JoshA328 Nov 30 '22 at 22:10
  • not sure why you are using filter when you say you are mapping. What should the final output look like? – Kinglish Nov 30 '22 at 22:12
  • @kinglish the final output should be an array (tempArr) that contains multiple arrays (view) grabbed from masterKey. Every key that outputReference contains should match with masterKey and grab the corresponding 'view'. As for me saying mapping, this was just incorrect terminology on my end. – JoshA328 Nov 30 '22 at 22:19

2 Answers2

0

If I understood correctly, your end goal is to insert in each position of the array tempArr the value of the property "view" nested inside of the first level properties of the masterKey object. But you only want to push to the tempArr keys that are contained inside of the OutputReference object. The code below will push the arrays inside of each view property nested in the properties Key1 and Key2 into tempArray.

const OutputReference = {
  Key1: "Some Random String",
  Key2: "Some Random String"
}
const masterKey = {

  'Key1': {
    Label: "Key 1",
    view: [1, 2, 3],
  },
  'Key2': {
    Label: "Key 2",
    view: [4, 5, 6],
  }
}
const tempArr = []
for (keys in OutputReference) {
  if (Object.hasOwn(masterKey, keys)) {
    tempArr.push(masterKey[keys]["view"])
    console.log('The following content is being pushed to the array: ', masterKey[keys]["view"])
  }
}
console.log('This is the content of tempArray:', tempArr)

Alternatively you can use

const OutputReference = {
  Key1: "Some Random String",
  Key2: "Some Random String"
}
const masterKey = {

  'Key1': {
    Label: "Key 1",
    view: [1, 2, 3],
  },
  'Key2': {
    Label: "Key 2",
    view: [4, 5, 6],
  }
}
const tempArr = []
Object.keys(OutputReference).forEach((key, value) => {
  if (Object.hasOwn(masterKey, key)) {
    tempArr.push(masterKey[key]["view"])
  }
  console.log('The following content is being pushed to the array: ', masterKey[key]["view"])
})

console.log('This is the content of tempArray:', tempArr)

I hope this works for you

0

You were close, but instead of filter, use map on the object keys to create the array

tempArr = Object.keys(OutputReference).map(m => masterKey[m].view)

const OutputReference = {
  Key1: "Some Random String",
  Key2: "Some Random String"
}
const masterKey = {
  'Key1': {
    Label: "Key 1",
    view: [1, 2, 3],
  },
  'Key2': {
    Label: "Key 2",
    view: [4, 5, 6],
  },
  'Key3': {
    Label: "Key 3",
    view: [4, 5, 6],
  },
}

const tempArr = Object.keys(OutputReference).map(m => masterKey[m].view)

console.log(tempArr)
Kinglish
  • 23,358
  • 3
  • 22
  • 43