javascript - Replace all keys in a JSON object one by one with my own data - Stack Overflow
I have a JSON object with the following content:
[{
"type": "pdf",
"size": "1mb",
"date": "392017",
.....500 more key value pairs like this
}]
This is how I am displaying my above JSON object on the screen in React Native.
export const MyComponent = ({myJson}) => {
const data = Object.entries(myJson).map(([key, value], i) => {
if (value !== '' && value !== null) {
return (
<View style={styles.body} key={i}>
<Text style={styles.labels}>{key}</Text>
<Text style={styles.values}>{value}</Text>
</View>
);
}
});
return (
<View>
{data}
</View>
);
};
I want to change the keys so it would bee:
[{
"File Type": "pdf",
"File Size": "1mb",
"Date Is": "392017",
.....500 more key value pairs like this
}]
How would I do that in React?
Thanks!
I have a JSON object with the following content:
[{
"type": "pdf",
"size": "1mb",
"date": "392017",
.....500 more key value pairs like this
}]
This is how I am displaying my above JSON object on the screen in React Native.
export const MyComponent = ({myJson}) => {
const data = Object.entries(myJson).map(([key, value], i) => {
if (value !== '' && value !== null) {
return (
<View style={styles.body} key={i}>
<Text style={styles.labels}>{key}</Text>
<Text style={styles.values}>{value}</Text>
</View>
);
}
});
return (
<View>
{data}
</View>
);
};
I want to change the keys so it would bee:
[{
"File Type": "pdf",
"File Size": "1mb",
"Date Is": "392017",
.....500 more key value pairs like this
}]
How would I do that in React?
Thanks!
Share Improve this question edited Mar 10, 2017 at 4:25 Testaccount asked Mar 10, 2017 at 3:45 TestaccountTestaccount 2,9113 gold badges24 silver badges28 bronze badges 1- You would likely provide a map of old name to new, then simply assign the old property to a new property with the new name, either in a new object or in the old one then delete the old property. What have you tried? – RobG Commented Mar 10, 2017 at 3:48
3 Answers
Reset to default 2Object.keys(yourObject).forEach((key)=>{ //this will return array of keys
yourObject[newKey] = yourObject[key]; //saving reference in your new key.
delete yourObject[key]; //deleting old key;
});
You will need to map you old keys with new ones, and then in loop you and easily change this.
Assuming that the var array is your JSON array and newArray is the array that you want get here is a code that can help you.
var array = [{
"type": "pdf",
"size": "1mb",
"date": "392017",
.....500 more key value pairs like this
}]
var newArray = [];
newArray = array.map(function(item){
return {
"File Type": item.type,
"File Size": item.size,
"Date is": item.date
}
})
The map method will transform every item in the array and result for another array.
Create a name map of oldName:newName and use it to map to an object with the new property names, e.g.
var data = [{
"type": "pdf",
"size": "1mb",
"date": "392017"
}];
var nameMap = {type:'File Type',
size:'File Size',
date:'Date Is'
};
var renamed = [Object.keys(data[0]).reduce(
(acc, key) => {
acc[nameMap[key]] = data[0][key];
return acc;
}, {})];
console.log(renamed);
- 京东1.7亿美元投资金蝶原因:布局企业ERP市场
- Intel毫不客气:ARM+Win8软硬件都不行
- python - Adding quotes to list objects to format as a dictionary pyspark - Stack Overflow
- python - How to send multiple images to azure custom vision api without going over the transaction limit of azure? - Stack Overf
- opengl - How to render a FBO into an ImGui window? - Stack Overflow
- swiftui - Ambiguous Init warning that doesn't make sense - Stack Overflow
- shell - Does the `hash` builtin command in Bash has an `-l` option? - Stack Overflow
- ios - AccessorySetupKit picker doesn't show accessory unless previously paired in Setting app first - Stack Overflow
- math - How do I rotate sprites by using polar coordinates in Godot 4.3? - Stack Overflow
- Trouble installing homebrew with ansible playbook, linux client and host nodes - Stack Overflow
- c++ - Microbenchmark - backward iteration results in fewer cache misses - Stack Overflow
- python - CERT_PATH points to the wrong directory even though I set it correctly in my Django project - Stack Overflow
- Azure monitor alert using minimum failing periods for static threshold - Stack Overflow
- rust - Remove struct from vector while mutably iterating through it - Stack Overflow
- javascript - mapBox autofill sessionToken usage? - Stack Overflow
- tsconfig - Is there a typescript compiler option that prevents implicit widening from readonly properties to readwrite? - Stack
- java - Is this a bug in the JVM, or in NASM? - Stack Overflow