0

I am a JS newbie trying to extract some value from an array of json maps. The map is something like:

var  tags = [{
    Key: 'backup',
    Value: 'true'
  },
  {
    Key: 'Name',
    Value: 'sdlc-root'
  }
]

// Here is my first attempt:

var volName = tags.filter(function(item) {
    return item.Key === 'Name';
  })
  .map(result => {
    return result.Value;
  });
console.log(volName);

The result is: [ 'sdlc-root' ] , but I only need the String value.

The temporary solution I take for now is:

var volName = tags.filter(function(item) { return item.Key === 'Name'; })
                  .map(result => { return result.Value; })**[0]**;
console.log(volName);    

The result is: sdlc-root

I hate my temporary solution, and would like to hear some advice for improvement or alternatives from experienced developers

5
  • do you have only one element inside? Commented Dec 18, 2018 at 9:13
  • Easy solution: destructure var [volName] = More elegant solution: use .find Commented Dec 18, 2018 at 9:14
  • @NinaScholz I have multiple elements, above is just example, tks. Commented Dec 18, 2018 at 9:23
  • @CertainPerformance not sure if my JS version support 'find', I think it's relatively new? Will definitely give a try. tks. Commented Dec 18, 2018 at 9:24
  • Don't let obsolete browsers cripple your ability to write concise, clean code - if an environment doesn't support ES6, use Babel + polyfills. Commented Dec 18, 2018 at 9:25

3 Answers 3

5

You could find the element or a default object and take the wanted property.

var volName = (tags.find(({ Key }) => Key === 'Name') || {}).Value;
Sign up to request clarification or add additional context in comments.

4 Comments

This is going to err if it doesn't find the correct value.
@chŝdk, you get from an empty object with a not given property the value undefined. this does not throw an error.
Why the ({Key}) - this seems easier to understand, no? var volName = (tags.find(x => x.Key === "Name") || {}).Value has less brackets
@mplungjan i think, if you need only a single property, you could destructure this property and show that you use only this property. it prevents as well an assignment to the object, if the value is a primitive value.
3

Write a custom function like below

var tags = [{
    Key: 'backup',
    Value: 'true'
  },
  {
    Key: 'Name',
    Value: 'sdlc-root'
  }
]

function f(tags) {
  for (i = 0; i <= tags.length; i++) {
    if (tags[i] && tags[i]['Key'] === 'Name') {
      return tags[i]['Value']
    }
  }
}

console.log(f(tags))

Comments

0
const tagsObj = tags.reduce((a, c) => { a[c.Key] = c.Value; return a }, {})
// {backup: "true", Name: "sdlc-root"}
console.log(tagsObj["Name"])
// "sdlc-root"

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.