简体   繁体   中英

Get array parent key or combine with nested items

I have the following object with arrays:

{
  "brands": [
    "accessible-icon",
    "accusoft",
    "adn",
    "adobe",
    "adversal",
  ],
  "regular": [
    "address-book",
    "address-card",
    "angry",
    "bell",
  ],
  "solid": [
    "ad",
    "address-book",
    "address-card",
    "adjust",
    "air-freshener",
    "align-center",
  ]
}

I have a function which returns one item from one of the array groups. I have to find out which array group the item was pulled from. From what I understood, it's not possible to look up parent keys in js?

Getting the parent key would be ideal, otherwise, is it possible to add the parent key to all the nested items? For example the nested items in the brands array would become:

 "brands": [
    "brands-accessible-icon",
    "brands-accusoft",
    "brands-adn",
    "brands-adobe",
    "brands-adversal",
  ],

Then I can use regex to extract brands .

More info:

Initially I'm transforming objects into object with array groups. Here is an example of one of the objects named brands :

{
  "faAccessibleIcon": {
    "prefix": "fab",
    "iconName": "accessible-icon",
    "icon": [
      448,
      512,
      [],
      "f368",
    ]
  },
  "faAccusoft": {
    "prefix": "fab",
    "iconName": "accusoft",
    "icon": [
      640,
      512,
      [],
      "f369",
    ]
  },
  "faAcquisitionsIncorporated": {
    "prefix": "fab",
    "iconName": "acquisitions-incorporated",
    "icon": [
      344,
      512,
      [],
      "f6af",
    ]
  },
}

Then I simplify the objects:

const array = {
  "brands": Object.keys(brands).map(e=> brands[e].iconName),
  "regular": Object.keys(regular).map(e=> regular[e].iconName),
  "solid": Object.keys(solid).map(e=> solid[e].iconName),
};

And array becomes the object with arrays seen at the top of my question. The array structure must remain like that. Is there a way to combine prefix and iconName and get brands-accessible-icon , brands-accusoft , etc, in the nested array items, for each group of arrays?

You could get the keys of the object and find the value in the array.

 const getKey = (object, value) => Object.keys(object).find(k => object[k].includes(value)); var data = { brands: ["accessible-icon", "accusoft", "adn", "adobe", "adversal"], regular: ["address-book", "address-card", "angry", "bell"], solid: ["ad", "address-book", "address-card", "adjust", "air-freshener", "align-center"] }; console.log(getKey(data, "bell")); 

I think there is an easy answer to it.

 const array = { "brands": Object.keys(brands).map(e=> "brands-"+brands[e].iconName), "regular": Object.keys(regular).map(e=> "regular-"+regular[e].iconName), "solid": Object.keys(solid).map(e=> "solid-"+solid[e].iconName), }; 

Here you go.

By getting the entries of your objects and then mapping every object to add their parent's name into their string, you can create your desired output.

(Unless I misunderstood your question since every answer seems to do something different)

Working example :

 const data = { "brands": [ "accessible-icon", "accusoft", "adn", "adobe", "adversal", ], "regular": [ "address-book", "address-card", "angry", "bell", ], "solid": [ "ad", "address-book", "address-card", "adjust", "air-freshener", "align-center", ] } const result = Object.entries(data).reduce((acc, [key, value]) => ({ ...acc, [key]: value.map(obj => key + '-' + obj) }), {}) console.log(result) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM