1

In Javascript, how to retrieve an object in an array by one of its property ?

Hi all, let's assume that we have the below :

   "Attributes":[
       {
           "Name":"Brief",
           "Value":"This skirt was fabriced from ...."
       },
       {
           "Name":"Details",
           "Value":"Measurements and Pictures are real"
       },
       {
           "Name":"SKUNumber",
           "Value":"12345678"
       }
   ]

What I need to do is to get the value of "Value" based on "Name"..

For example :

console.log(Attributes.Brief)   ==> "This skirt was fabriced from ...."

So I need a function to help doing that

Note that I don't want to use the index of the object, because its order may changed.

Thank you

Christophe Quintard
  • 1,858
  • 1
  • 22
  • 30
Saif Obeidat
  • 128
  • 1
  • 2
  • 16
  • Possible duplicate of [Find object by id in an array of JavaScript objects](https://stackoverflow.com/questions/7364150/find-object-by-id-in-an-array-of-javascript-objects) – Christophe Quintard Nov 22 '18 at 12:28

4 Answers4

2

Well, it's always better to show what you have attempted rather than just asking..

You can use Array.find to achieve this

let Attributes = [
                     {
                        "Name":"Brief",
                        "Value":"This skirt was fabriced from ...."
                     },
                     {
                        "Name":"Details",
                        "Value":"Measurements and Pictures are real"
                     },
                     {
                        "Name":"SKUNumber",
                        "Value":"12345678"
                     }
                  ]
                  
function getValueByName(name) {
  return Attributes.find(d => d.Name.toLowerCase() == name.toLowerCase()).Value
}

console.log(getValueByName('Brief'))
console.log(getValueByName('details'))
console.log(getValueByName('SKUNumber'))
Nitish Narang
  • 4,124
  • 2
  • 15
  • 22
1

One option you have is to use Array.prototype.filter:

const d = [{
    "Name": "Brief",
    "Value": "This skirt was fabriced from ...."
  },
  {
    "Name": "Details",
    "Value": "Measurements and Pictures are real"
  },
  {
    "Name": "SKUNumber",
    "Value": "12345678"
  }
]

console.log(d.filter(x=>x.Name==="Brief")[0].Value)

You can also make it more generic:

const d = [{
    "Name": "Brief",
    "Value": "This skirt was fabriced from ...."
  },
  {
    "Name": "Details",
    "Value": "Measurements and Pictures are real"
  },
  {
    "Name": "SKUNumber",
    "Value": "12345678"
  }
]

const getValOfXfromArrByValOfY = (arr, x, y, val) => arr.find(z => z[y] === val)[x]

console.log(getValOfXfromArrByValOfY(d, 'Value', 'Name', 'SKUNumber'))
connexo
  • 53,704
  • 14
  • 91
  • 128
1

You could use a Proxy with a getter for the key, which returns a find of the object with the value.

var object = { attributes: [{ Name: "Brief", Value: "This skirt was fabriced from ...." }, { Name: "Details", Value: "Measurements and Pictures are real" }, { Name: "SKUNumber", Value: "12345678" }] },
    attributes = new Proxy(
        object.attributes,
        { get: (array, prop) => (array.find(({ Name }) => Name === prop) || {}).Value }
    );
   
console.log(attributes.Brief);
console.log(attributes.SKUNumber);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • Clever. Worth mentioning that IE does not support this at all, though, despite all other major browsers do. – briosheje Nov 22 '18 at 12:33
  • right, it's ES6, as well as `find`, which uses all other answers as well without mentioning. – Nina Scholz Nov 22 '18 at 12:34
  • True, indeed. I just wanted to mention that since I don't see `Proxy` used that often, to be entirely honest I never saw that at all. – briosheje Nov 22 '18 at 12:52
0

You can use javascript find function see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find see bellow sample code:

 var Attributes =[
               {
                   "Name":"Brief",
                   "Value":"This skirt was fabriced from ...."
               },
               {
                   "Name":"Details",
                   "Value":"Measurements and Pictures are real"
               },
               {
                   "Name":"SKUNumber",
                   "Value":"12345678"
               }
           ]

            var found = Attributes.find(function(element) {
              return element.Name == "Details";
            });

console.log(found.Value); //output : Measurements and Pictures are real
Voice Of The Rain
  • 495
  • 1
  • 8
  • 24