Home > database >  Access an Object within an Object by the Key of another Object
Access an Object within an Object by the Key of another Object

Time:12-01

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])

CodePudding user response:

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

CodePudding user response:

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)

  • Related