最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert specific value in array of objects to lowercase - Stack Overflow

programmeradmin1浏览0评论

I am trying to figure out how to convert every value of a specific key in an array of objects to lowercase. For example I want to lowercase every value with the key 'Domain':

var array = [
   { "Name" : "Bill":,
     "Domain" : "TEST",
   },
   { "Name" : "John":,
     "Domain" : "JohnTest",
   },
   { "Name" : "Fred":,
     "Domain" : "fredtest",
   }
]

Would convert to:

var newArray = [
   { "Name" : "Bill":,
     "Domain" : "test",
   },
   { "Name" : "John":,
     "Domain" : "johntest",
   },
   { "Name" : "Fred":,
     "Domain" : "fredtest",
   }
]

I know how to use the toLowerCase() with strings and arrays, but I can't seem to figure out how to map over an array of objects to do this. I was trying something along the lines of this:

var newArray = array.map(function(i) {
    return i.Domain.toLowerCase;

})

I am trying to figure out how to convert every value of a specific key in an array of objects to lowercase. For example I want to lowercase every value with the key 'Domain':

var array = [
   { "Name" : "Bill":,
     "Domain" : "TEST.",
   },
   { "Name" : "John":,
     "Domain" : "JohnTest.",
   },
   { "Name" : "Fred":,
     "Domain" : "fredtest.",
   }
]

Would convert to:

var newArray = [
   { "Name" : "Bill":,
     "Domain" : "test.",
   },
   { "Name" : "John":,
     "Domain" : "johntest.",
   },
   { "Name" : "Fred":,
     "Domain" : "fredtest.",
   }
]

I know how to use the toLowerCase() with strings and arrays, but I can't seem to figure out how to map over an array of objects to do this. I was trying something along the lines of this:

var newArray = array.map(function(i) {
    return i.Domain.toLowerCase;

})
Share Improve this question edited Jan 5, 2018 at 15:24 Andreas 21.9k7 gold badges51 silver badges58 bronze badges asked Jan 5, 2018 at 15:22 MattMcCodeMattMcCode 3077 silver badges20 bronze badges 3
  • Do you want to mutate the original objects or fill the new array with (deep-)copies? – le_m Commented Jan 5, 2018 at 15:24
  • Sorry I'm not pletely sure what you mean, I'm fairly new to javascript. I just want the exact same array returned, just with lowercase domains. It doesn't necessarily have to be put into it's own new array – MattMcCode Commented Jan 5, 2018 at 15:26
  • This works, but the answer from @mersocarlin has the advantage of working with objects with many more properties, without changing code. – Scott Sauyet Commented Jan 5, 2018 at 15:50
Add a ment  | 

5 Answers 5

Reset to default 5

map returns another array and since each item of your array is an object you need to return an object on each iteration as well:

const array = [
   { "Name" : "Bill:",
     "Domain" : "TEST.",
   },
   { "Name" : "John:",
     "Domain" : "JohnTest.",
   },
   { "Name" : "Fred:",
     "Domain" : "fredtest.",
   }
]

const result = array.map(item => ({
  ...item,
  Domain: item.Domain.toLowerCase()
}))

console.log(result)

You can use map like this:

var newArray = array.map(function(elem, i) {
    return {
        Name: elem.Name,
        Domain: elem.Domain.toLowerCase()
    };
});

When you used the array.map function, your first parameter on the inside function was i, but hopefully you can understand via my example that the first parameter is actually the element in the array, and the 2nd parameter is the 'index'.

Your object structure has some syntax errors. But after fixing them, you can just enumerate the objects in the array and reset the Domain property to the lowercase version.

var array = [
   { "Name" : "Bill",
     "Domain" : "TEST."
   },
   { "Name" : "John",
     "Domain" : "JohnTest."
   },
   { "Name" : "Fred",
     "Domain" : "fredtest."
   }
]

// Enumerate the array items
array.forEach(function(obj){
  // Set the Domain property of the current object being enumerated
  // to the lowercase version of itself
  obj.Domain = obj.Domain.toLowerCase();
});

console.log(array);

First of all delete ":" after every value in object. Second you should call toLowerCase method by "()"

function mapper(array) {
      return array.map(function(i) {
         i.Domain = i.Domain.toLowerCase();
         return i;
    })
}

var array = [
   { "Name" : "Bill",
     "Domain" : "TEST.",
   },
   { "Name" : "John",
     "Domain" : "JohnTest.",
   },
   { "Name" : "Fred",
     "Domain" : "fredtest.",
   }
]

var newArray = mapper(array);

console.log(newArray); /*[ { Name: 'Bill', Domain: 'test.' },
    { Name: 'John', Domain: 'johntest.' },
    { Name: 'Fred', Domain: 'fredtest.' } ]*/
    var newArray = [
    {
        "Name": "Bill",
        "Domain": "TesT.",
    },
    {
        "Name": "John",
        "Domain": "Tohntest.",
    },
    {
        "Name": "Fred",
        "Domain": "FredTe`enter code here`st.",
    }
]

newArray = newArray.map(function (data) {
    return {
        Name: data.Name,
        Domain: data.Domain.toLowerCase().trim()
    }
})

console.log(newArray)

you can try this way

发布评论

评论列表(0)

  1. 暂无评论