How to sort a multidimensional object in JavaScript [duplicate] - javascript

This question already has answers here:
Sorting Object by sub-object property
(4 answers)
Closed 7 years ago.
How to sort the objects by age value?
I have the following object structure
{
"men": {
"20114": {
"id": "20114",
"name": "Peter",
"age": "21"
},
"28957": {
"id": "28957",
"name": "Paul",
"age": "20"
}
},
"women": {
"8957": {
"id": "8957",
"name": "Rose",
"age": "24"
},
"2178": {
"id": "2178",
"name": "Sara",
"age": "22"
}
},
}
I know, that I can sort arrays like this
groups.sort(function(a, b) {
return b.age - a.age;
});
but how to do this with objects?

It would be a lot easier to sort your data if you could change your structure to the JSON model below:
var data = [
{
"id": "20114",
"name": "Peter",
"age": "21",
"gender": "men"
},
{
"id": "28957",
"name": "Paul",
"age": "20",
"gender": "men"
},
{
"id": "8957",
"name": "Rose",
"age": "24",
"gender": "women"
},
{
"id": "2178",
"name": "Sara",
"age": "22",
"gender": "women"
}
]
data.sort(function(a, b) {
return parseFloat(a.age) - parseFloat(b.age);
});
data.sort()
document.write(JSON.stringify(data))

function sortfunc(prop){
return function(obj1,obj2){
var val1 = obj1[prop];
var val2 = obj2[prop];
return val1 - val2;
};
}
groups.sort(sortfunc(prop));
pass prop as property name

Related

How to remove a child object from a JSON object using javascript

I'm trying to search through a JSON object to find the pet-rock that I want to delete. Here is a simple JSON that I'm working on:
myData.json:
{
"data": [
{
"name": "John",
"age": "25",
"pet-rocks": [
{
"name": "Travis",
"age": "9"
},
{
"name": "Steven",
"age": "5"
},
{
"name": "Oliver",
"age": "7"
}
]
},
{
"name": "Jane",
"age": "25",
"pet-rocks": [
{
"name": "Jesse",
"age": "4"
},
{
"name": "Carol",
"age": "8"
},
{
"name": "Jake",
"age": "7"
}
]
}
]
}
I would like to do a search for "Steven" and remove that pet-rock from the list. Here are the things I've tried:
MyJSFile.js:
const myDataObject = require('./myData.json');
for (let key in myDataObject) {
let value = myDataObject[key];
if (value === "Steven")
{
delete myDataObject[key];
}
//To see if I get the data I want
console.log(key, value);
}
However, my output is strange and I'm not sure how to get to that child node of petrock. It appears that the petrocks are in object form, and I'm not sure how to get to them. here is the ouput of my console below. I assume it didn't get delete as there are still 3 petrock objects in the data.
data [
{
name: 'Robert',
age: '25',
'pet-rocks': [ [Object], [Object], [Object] ]
},
{
name: 'Robert',
age: '25',
'pet-rocks': [ [Object], [Object], [Object] ]
}
]
Any help or suggestions would be greatly appreciated! Thanks!
The pet rock named Steven is not a direct child of myDataObject, so you can't delete it like that. You can loop through the "data" array, rebuilding the "pet-rocks" array for each element. A simple filter to remove any pet rocks named Steven should work.
const myDataObject = {
"data": [
{
"name": "John",
"age": "25",
"pet-rocks": [
{
"name": "Travis",
"age": "9"
},
{
"name": "Steven",
"age": "5"
},
{
"name": "Oliver",
"age": "7"
}
]
},
{
"name": "Jane",
"age": "25",
"pet-rocks": [
{
"name": "Jesse",
"age": "4"
},
{
"name": "Carol",
"age": "8"
},
{
"name": "Jake",
"age": "7"
}
]
}
]
};
myDataObject.data.forEach(d => {
d["pet-rocks"] = d["pet-rocks"].filter(rock => rock.name !== "Steven");
});
console.log(myDataObject);

Comparing 2 objects and making alterations based on Match

I have 2 objects with key-value pairs that should always be identical (match) otherwise I want to modify the value of the key in object #1 to "Some Value - Not available"
Here are my 2 objects:
Object #1
[
{
"name": "John",
"age": "12",
"id": 1
},
{
"name": "tina",
"age": "19",
"id": 2
}]
Object #2 ( name checker)
[
{
"value": "tina"
},
{
"value": "Trevor"
},
{
"value": "Donald"
},
{
"value": "Elon"
},
{
"value": "Pamela"
},
{
"value": "Chris"
},
{
"value": "Jackson"
}
]
I would like to find out if name in Object #1 is found in Object #2 and if it is not found, add " - not available"
e.i
[
{
"name": "John - not available",
"age": "12",
"id": 1
},
{
"name": "tina",
"age": "19",
"id": 2
}
]
Important Note: I'm using ES5 --- not ES6
This should work in ES5
object1.forEach(function(person) {
var found = false;
Object.keys(object2).forEach(function(key) {
if (person.name === object2[key].value) {
found = true;
}
});
if (!found) {
person.name = person.name + " - not available";
}
});

get all values of a specific key in an array of dictionaries - javascript [duplicate]

This question already has answers here:
From an array of objects, extract value of a property as array
(24 answers)
Closed 2 years ago.
I have this json data below:
[{
"name": "Dad",
"age": "32"
}, {
"name": "Mom",
"age": "30"
}, {
"name": "Son",
"age": "3"
}]
I would like to have an array: ["Dad", "Mom", "Son"].
What is the correct way to implement this in Javascript?
This is one way:
var result= [{
"name": "Dad",
"age": "32"
}, {
"name": "Mom",
"age": "30"
}, {
"name": "Son",
"age": "3"
}].map( item => { return item.name }); //["Dad", "Mom", "Son"]
console.log(result);
var result= [{
"name": "Dad",
"age": "32"
}, {
"name": "Mom",
"age": "30"
}, {
"name": "Son",
"age": "3"
}].map( function(item) { return item.name }); //["Dad", "Mom", "Son"]
console.log(result);

Append Multiple objects into single array

I have an array of objects of the structure coming from server response of iterated array object like as sample
array[1] ={
"ID": "123",
"Name": "John",
"Age": "15"
}
array[2] ={
"ID": "456",
"Name": "Sue",
"Age": "18"
}
array[n] ={
}
But now I want to append the array values if the condition age below 18 in the following structure of as iterated values of above array
Expected Output:
{
"Stud": [{
"ID": "123",
"Name": "John",
"Age": "15"
}, {
"ID": "456",
"Name": "Sue",
"Age": "18"
},{n........
}]
}
var output = { "Stud" : [] };
for (var i = array.length - 1; i >= 0; i--) {
if (array[i].Age < 18) {
output.Stud.push(array[i]);
}
}
console.log(output);
Simply
var output = { "Stud" : array }; //existing 'array'
You can use array#filter to select objects with age less than or equal to 18.
const response = [{
"ID": "123",
"Name": "John",
"Age": "15"
},{
"ID": "456",
"Name": "Sue",
"Age": "18"
},{
"ID": "459",
"Name": "Jaby",
"Age": "20"
}];
const result = response.filter(o => o.Age <= 18);
var output = {'stud' : result};
console.log(output);
Simply iterate and check whether the age is < 18 or not. If so push that to another array.
var array = [];
var array_below_18 = [];
array[0] ={
"ID": "123",
"Name": "John",
"Age": "15"
}
array[1] ={
"ID": "456",
"Name": "Sue",
"Age": "18"
}
array[1] ={
"ID": "456",
"Name": "Sue",
"Age": "14"
}
for(i=0;i<array.length;++i){
if(parseInt(array[i].Age)<18){
array_below_18.push(array[i]);
}
}
var final_object = {
"Stud" : array_below_18
}
console.log(final_object);

How to add a name for each element of a list in Jackson

Is it possible to set a name for each element of a list in JSON using JACKSON?
For example, I have the following JSON:
{"result": [
{
"name": "ABC",
"age": "20"
},{
"name": "DEF",
"age": "12"
}
]}
But I need this:
{"result": [
person: { // << this is the name
"name": "ABC",
"age": "20"
},
person: {
"name": "DEF",
"age": "12"
}
]}
Thanks to everybody!
UPDATE
Hi people!
I made a mistake! The correct form is the following:
{"result": [
{
person: { // << this is the name
"name": "ABC",
"age": "20"
}
},
{
person: {
"name": "DEF",
"age": "12"
}
}
]}
In plain Javascript, you could use Array#map and return a new object with person property.
var object = { result: [{ name: "ABC", age: "20" }, { name: "DEF", age: "12" }] };
object.result = object.result.map(function (a) {
return { person: a };
});
console.log(object);

Categories

Resources