'How can I make a JSON Object containing two properties from a JSON Object containing four properties
I have a JSON object like this:
[
{'car':'punto','brand':'fiat','color':'black','model':'2007'},
{'car':'XUV500','brand':'Mahindra','color':'black','model':'2008'},
{'car':'Eartiga','brand':'Maruti','color':'black','model':'2009'}
]
I want to achieve this:
[
{'car':'punto','brand':'fiat'},
{'car':'XUV500','brand':'Mahindra'},
{'car':'Eartiga','brand':'Maruti'}
]
How can I achieve it using javascript and Jquery. Any help isappreciated!
Solution 1:[1]
UPDATE 2022
You could just map the wanted properties by destructuring and short hand properties.
const
data = [{ car: 'punto', brand: 'fiat', color: 'black', model: '2007' }, { car: 'XUV500', brand: 'Mahindra', color: 'black', model: '2008' }, { car: 'Eartiga', brand: 'Maruti', color: 'black', model: '2009' }],
result= data.map(({ car, brand }) => ({ car, brand }));
console.log(result);
Depending on the need, you have basically two possibillities to tackle the problem:
- rebuild the array with new objects, keeps the original array;
- delete unwanted properties from each object, which modifies the original array.
var data = [{ 'car': 'punto', 'brand': 'fiat', 'color': 'black', 'model': '2007' }, { 'car': 'XUV500', 'brand': 'Mahindra', 'color': 'black', 'model': '2008' }, { 'car': 'Eartiga', 'brand': 'Maruti', 'color': 'black', 'model': '2009' }],
data1 = data.map(function (a) {
return { car: a.car, brand: a.brand };
});
data.forEach(function (a) {
delete a.color;
delete a.model;
})
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(data1, 0, 4) + '</pre>');
Solution 2:[2]
you can use map function in javascript
try this
x = [{
'car': 'punto',
'brand': 'fiat',
'color': 'black',
'model': '2007'
}, {
'car': 'XUV500',
'brand': 'Mahindra',
'color': 'black',
'model': '2008'
}, {
'car': 'Eartiga',
'brand': 'Maruti',
'color': 'black',
'model': '2009'
}]
var result = x.map(function(v) {
return {
car: v.car,
brand: v.brand
}
})
document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>')
Solution 3:[3]
You can loop through each item in the array and create a new array with the data you want.
var originalArray = [{
'car': 'punto',
'brand': 'fiat',
'color': 'black',
'model': '2007'
}, {
'car': 'XUV500',
'brand': 'Mahindra',
'color': 'black',
'model': '2008'
}, {
'car': 'Eartiga',
'brand': 'Maruti',
'color': 'black',
'model': '2009'
}];
var newArray = [];
for (var i = 0; i < originalArray.length; i++) {
var originalItem = originalArray[i];
newItem = {
car: originalItem.car,
brand: originalItem.brand
};
newArray.push(newItem);
}
console.debug(newArray);
Solution 4:[4]
var oldJson = [
{'car':'punto','brand':'fiat','color':'black','model':'2007'},
{'car':'XUV500','brand':'Mahindra','color':'black','model':'2008'},
{'car':'Eartiga','brand':'Maruti','color':'black','model':'2009'}
];
var newJson = [];
for(var key in oldJson) {
newJson.push({
'car' : oldJson[key].car,
'brand' : oldJson[key].brand
})
}
console.log(newJson);
Solution 5:[5]
Try this method :
function getIwanttoList() {
var data = [
{
'text': '----- Select what you want -----',
'value': '',
}, {
'text': 'Ask a question',
'value': 'AskAQuestion',
}, {
'text': 'Report a content issue',
'value': 'ReportContentIssue',
}, {
'text': 'Propose an idea or quality improvement',
'value': 'ProposeImprovements',
}, {
'text': 'Submit content for governance',
'value': 'SubmitContent',
}
];
return data;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | |
| Solution 3 | TwitchBronBron |
| Solution 4 | vqianduan |
| Solution 5 | vincrichaud |
