Changing the key name in an array of objects?
var i;
for(i = 0; i < arrayObj.length; i++){
arrayObj[i].stroke = arrayObj[i]['key1'];
delete arrayObj[i].key1;
}
Change some object keys on an array and maintain order
Maybe something like this:
const data = [
{
prop1: 'Change key but keep position',
second: 'Keep same key name and position',
prop3: 'Change key but keep position',
fourth: 'Keep same key name and position',
}
];
const renameMap = { prop1: 'first', prop3: 'third'};
const res = data.map(
obj => Object.fromEntries(Object.entries(obj).map(
([key, value]) => [renameMap[key] ?? key, value]
))
);
console.log(res);
Javascript - Change name of all object keys in nested arrays
You can do this by using Recursion.
Check if the value of the [key-value] pair from the Object#entries()
call is an object.
If so, call the transformObj
function again recursively for that value. Else return the value as is.
And finally convert the array of [key-value] pairs back to an object by using Object#fromEntries
:
const packages = [{ id: '641a1690-6c8b-4ada-ae97-8d82cc4fe7a3', name: 'com.sample', children: { id: 'd7384f60-e4ab-4a86-8e2e-0f66cc32f', name: 'child.computer.com', children: { id: 'e4ab-4a86-0f66cc32f560', name: 'child.com' }}}, { id: 'd7384f60-e4ab-4a86-8e2e-0f66cc32f560', name: 'computer.com' }, { id: 'ca7f972e-64ee-4cb0-80b9-1036fac69d32', name: 'java.util' }];
const replacer = { "id": "key", "name" :"title"};
const transformObj = (obj) => {
if(obj && Object.getPrototypeOf(obj) === Object.prototype){
return Object.fromEntries(
Object.entries(obj)
.map(([k, v]) => [replacer[k] || k, transformObj(v)])
);
}
//Base case, if not an Object literal return value as is
return obj;
}
console.log(packages.map(o => transformObj(o)));
es6 rename keys in object array
Use .map
to transform one array into another, and destructure the arguments for the least syntax noise:
const countries = [ {"id": 1, "name": "Afghanistan"}, {"id": 2, "name": "Albania"}, {"id": 3, "name": "Algeria"}, {"id": 4, "name": "American Samoa"}];const transformed = countries.map(({ id, name }) => ({ label: id, value: name }));console.log(transformed);
How to modify or replace key inside Array of Object in Javascript
Assuming you want to convert key i
to the i
th letter of the alphabet (and assuming you don't go beyond i = 25
), you can use String.fromCharCode
and the fact that 65
translates to A
:
const newArray1 = Object.fromEntries(
Object.entries(Array1).map(
([key, value]) => [String.fromCharCode(65 + parseInt(key)), value]
)
)
This will convert {0: {...}, 1: {...}, 2: {...}, 3: {...}}
or [{...}, {...}, {...}, {...}]
into {A: {...}, B: {...}, C: {...}, D: {...}}
.
How to rename key of object in an array
How about this?
var products = [{ text: 'prod1', value: 1 }, { text: 'prod2', value: 2 }, { text: 'prod3', value: 3 }];
products.forEach(function(obj) { obj.label = obj.text; delete obj.text;});console.log(products);
How to change all key name inside nested array of objects javascript
Fun fact: if you need to walk through an object for whatever reason, JSON.stringify() is your friend.
Not because you want to turn your data into a JSON string, but because it's also an object iterator that lets you perform arbitrary processing at every level using a replacer function:
const data = [
{text: 'node 1'},
{text: 'node 2', chapter_playlist: [{text: 'node 2-1', lesson_playlist: [{text: 'node 3-1'}]}]},
{text: 'node 3'},
{text: 'node 4', chapter_playlist: [{ text: 'node 4-1' }]}
]
const rewriteList = [`chapter_playlist`, `lesson_playlist`];
function replacer(key, value) {
// If the value at this depth is an object (but not an iterable
// like array or Set or Map), then rebind the properties you
// need rebound, provided they exist:
if (typeof value === `object` && !value[Symbol.iterator]) {
rewriteList.forEach(prop => {
if (value[prop]) {
// By editing "value", we're directly updating "data".
value.children = value[prop];
delete value[prop];
}
});
}
return value;
}
JSON.stringify(data, replacer);
console.log(data)
Related Topics
Jquery Add Class Based on Page Url
Convert JavaScript-Generated Svg to a File
How to Create Dynamic Elements Inside Global Tabs
How to Toggle Class Using Pure JavaScript in HTML
Most Elegant Way to Force a Textarea Element to Line-Wrap, *Regardless* of Whitespace
Getelementsbyclassname to Change the Style of Elements When Event Occurs
Convert CSS Width String to Regular Number
How to Stop User from Printing Webpages? Using JavaScript or Jquery
Bootstrap and Jqueryui Conflict
The Purpose of Starting an Initial Comment with /*! in JavaScript and CSS Files
Jquery Select Pseudo-Element :After
Add CSS Styled Marker to Google Maps
Mime Type Error with Express.Static and CSS Files
JavaScript Function to Convert Color Names to Hex Codes
Jquery to Change Style Attribute of a Div Class
Google Chrome Console.Log() Inconsistency with Objects and Arrays
Find All Possible Subset Combos in an Array
Regex Match Count of Characters That Are Separated by Non-Matching Characters