2

Array looks like this:

"myTags":
[{
  "type":"one",
  "value":"Testing",
  "note":"Hey"
},{
  "type":"two",
  "value":"Yg5GE",
  "note":"hey2"
}]

I need to convert type:'one' value 'Testing' into lowercase i.e. value = Testing needs to be 'testing'. Is there a way to so this keeping the same structure?

Note: "type":"one","value":"Testing", may not always be included in the array. So I guess in this scenario I first need to do a check if they exist?

I have tried .map, however I am unable to get the result I want. Any help would be appreciated.

Ideal structure:

  "myTags":
  [{
    "type":"one",
    "value":"testing",
    "note":"hey"
  },{
    "type":"two",
    "value":"Yg5GE",
    "note":"hey2"
  }]
noDe1
  • 331
  • 1
  • 5
  • 15
  • 3
    _"I have tried .map, however I am unable to get the result I want"_ - Then please add your attempt so we can help you understand and fix the problem. – Andreas Feb 05 '18 at 10:48
  • If the structure is flexible (that is, it's not always the same and you want to change *all* possibe values) you could consider it as a tree, traverse it, and change values when you reach a leaf. But without knowing what you already tried you may have already tried this. – Federico klez Culloca Feb 05 '18 at 10:49
  • related: https://stackoverflow.com/questions/12539574/whats-the-best-way-most-efficient-to-turn-all-the-keys-of-an-object-to-lower – Jigar Shah Feb 05 '18 at 10:49

3 Answers3

2

Iterate over the elements in myTags, check if type is "one" and only then change the content of value to lowercase (if present)

var data = {
  "myTags": [{
    "type": "one",
    "value": "Testing",
    "note": "Hey"
  }, {
    "type": "one",
    "note": "Hey"
  }, {
    "type": "two",
    "value": "Yg5GE",
    "note": "hey2"
  }]
};

data.myTags.forEach(function(tag) {
  if (tag.type === "one" && typeof tag.value !== "undefined") {
    tag.value = tag.value.toLowerCase();
  }
});

console.log(data.myTags);

You may also first filter the content of myTags to get the element(s) with "type": "one" and only iterate over those element(s)

var data = {
  "myTags": [{
    "type": "one",
    "value": "Testing",
    "note": "Hey"
  }, {
    "type": "one",
    "note": "Hey"
  }, {
    "type": "two",
    "value": "Yg5GE",
    "note": "hey2"
  }]
};

data.myTags
    .filter(function(tag) {
      return tag.type === "one";
    })
    .forEach(function(tag) {
      if (typeof tag.value !== "undefined") {
        tag.value = tag.value.toLowerCase();
      }
    });

console.log(data.myTags);
Andreas
  • 21,535
  • 7
  • 47
  • 56
0

This one works perfectly

 $(document).ready(function(){

 var objects ={"myTags":

 [{"type":"one","value":"Testing", "note":"Hey"}

 ,{ "type":"two", "value":"Yg5GE", "note":"hey2"}]}; 

  var obj = objects.myTags.map(function(a) { 
  a.value = a.value.toLowerCase();
  return a;

 });
 console.log(obj);
 });

output:

  {type: "one", value: "testing", note: "Hey"}

  {type: "two", value: "yg5ge", note: "hey2"}

Thank you

Sibin Rasiya
  • 1,132
  • 1
  • 11
  • 15
0

Achieve this very simply by using an arrow function and the map() method of the Array

var words = ['Foo','Bar','Fizz','Buzz'].map(v => v.toLowerCase());
console.log(words);
Surya R Praveen
  • 3,393
  • 1
  • 24
  • 25