How would I use _.mapKey over this array of objects

lodash object to array
lodash map
lodash filter array of objects
lodash find object in array
lodash merge objects
lodash filter object
javascript map object
lodash chain

I have the following array of objects. I would like to change the key "text" to be named label

[
  {id: 0, text: "blue"},

  {id: 1, text: "green"},

  {id: 2, text: "orange"},

  {id: 3, text: "yellow"}
]

The "id" has to stay as is.

I can use lodash or javascript.


You can use the .map() method to rename key in array of objects.

var data = [
  {id: 0, text: "blue"},

  {id: 1, text: "green"},

  {id: 2, text: "orange"},

  {id: 3, text: "yellow"}
]

data = data.map(el => {
  return {
    id: el.id,
    label: el.text
  }
})

console.log(data);

Array, Creates an array of array values not included in the other given arrays using SameValueZero for collection (Array|Object): The collection to iterate over. Reformatting Array Objects.map() can be used to iterate through objects in an array and, in a similar fashion to traditional arrays, modify the content of each individual object and return a new array. This modification is done based on what is returned in the callback function.


you can do using .map

let arr=[
  {id: 0, text: "blue"},

  {id: 1, text: "green"},

  {id: 2, text: "orange"},

  {id: 3, text: "yellow"}
];

let arr1=[];
arr1=arr.map(a=>({id:a.id,label:a.text}));
console.log(arr1);

mapValues for objects · Issue #169 · lodash/lodash · GitHub, I'll reproduce the code from jashkenas/underscore#220 he but depend on libraries like this to smooth over the wonky bits of the semantics. objects and arrays act very similar, but then so do 0 and -0. :) _.mixin({ 'mapKeys': function mapValues(object, callback, thisArg) { var result = {}; callback = _. What you need is to map your array of objects and remember that every item will be an object, so that you will use for instance dot notation to take the values of the object. And remember when you put an array of jsx it has a different meaning and you can not just put object in your render method as you can put an array.


You can use the function forEach and an array with the targets

This alternative will mutate the original array.

let array = [{id: 0, text: "blue"},{id: 1, text: "green"}, {id: 2, text: "orange"},  {id: 3, text: "yellow"}],
    target = [['text', 'label'], ['ele', 'stack']];

array.forEach(o => {      
  target.forEach(([src, dest]) => {
    if (o[src]) {
      Object.assign(o, {[dest]: o[src]});
      delete o[src];
    }
  });
});
    
console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

_.map(object, ) fails when object contains a 'length , mapKeys or _. like _.tranform, which will iterate through all the keys of the object.: _.transform(object, function(array, count, word) { array.push({ 'count': count Using attribute name "length" in schema causes serializer to fail  Object.entries() returns an array whose elements are arrays corresponding to the enumerable property [key, value] pairs found directly upon object. The ordering of the properties is the same as that given by looping over the property values of the object manually.


Iterating over objects in Lodash - Today I Learned, Many of the Lodash collection functions iterate over either an object or an array. Somewhat unintuitively for me, when iterating over objects the  Checks if any item in an array passes the condition. A good use case would be checking for user privileges. It can also be used similarly to a .forEach() where you would perform an action on each array item and break out of the loop once a truthy value is returned.


Map and Set, Although map[key] also works, e.g. we can set map[key] = 2 , this is treating map as a Using objects as keys is one of most notable and important Map features. For looping over a map , there are 3 methods: then we can use built-in method Object.entries(obj) that returns an array of key/value pairs for  callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed. If a thisArg parameter is provided to map, it will be used as callback's this value. Otherwise, the value undefined will be used as its this value.


Object.keys, values, entries, Object.entries(obj) – returns an array of [key, value] pairs. Here's an example of using Object.values to loop over property values: and then fromEntries gives back the object Object . entries ( prices ) . map ( ( [ key , value ]  The map() method creates a new array with the results of calling a function for every array element. The map() method calls the provided function once for each element in an array, in order. Note: map() does not execute the function for array elements without values. Note: this method does not change the original array.