Converting object of array values into array of objects - javascript

Cant seem to find any threads that talk about this type of data structure manipulation.
I have a new API schema for a service that I am replacing. The original API returned an array of objects.
[
{name: 'Conner', age: 24, gender: 'male'},
{name: 'Bryan', age: 32, gender: 'male'}
]
The new response looks like..
{
fields: {
name: {
values: [
'Conner',
'Bryan'
]
},
age: {
values: [
24,
32
]
},
gender: {
values: [
'male',
'male'
]
}
}
}
What would be the best/most minimal way to transform the response to the existing format: an array of objects. I can use vanilla javascript or Lodash.

I've made it tick like you want it to by taking the length of the values of the name prop, in order to determine how many iterations/people we have.
Still, this is a very peculiar data set - but here goes:
const fields = {
name: {
values: [
'Conner',
'Bryan'
]
},
age: {
values: [
24,
32
]
},
gender: {
values: [
'male',
'male'
]
}
}
const previous = fields.name.values.map((item, i) => {
return Object.keys(fields).reduce((obj, key) => {
obj[key] = fields[key].values[i]
return obj
}, {})
})
console.log(previous)

Related

Check if objects in array contain equal properties

I have an array of objects, each with various properties. I want to check if one particular property is equal across all of the objects. e.g.
peopleArr = [
{
name: Simon,
age: 22,
hair: brown
},
{
name: John,
age: 22,
hair: black
},
{
name: James,
age: 22,
hair: blond
}
]
I need a function that returns true if age has the same value across all of the objects in the array, and false if not. I've tried some variations using .every, but can't get it to work with object properties specifically (I'm relatively new). Any help appreciated.
You can use array every method and inside the callback check if age in all the object is equal to 22. It will return Boolean value and it will return true if all all the object matches the condition
const peopleArr = [{
name: 'Simon',
age: 22,
hair: 'brown'
},
{
name: 'John',
age: 22,
hair: 'black'
},
{
name: 'James',
age: 23,
hair: 'blond'
}
]
const res = peopleArr.every(item => item.age === 22);
console.log(res)
An alternative to using the .every() method, would be to filter the array and compare the filtered array length to the original array length. Like so
const peopleArr = [
{
name: "Simon",
age: 22,
hair: "brown"
},
{
name: "John",
age: 22,
hair: "black"
},
{
name: "James",
age: 22,
hair: "blond"
}
]
const array_val_same = ( arr, val ) => {
let filtered = arr.filter(el => el.age === val)
return filtered.length === arr.length ? true : false
}
array_val_same(peopleArr, 22)
This is just an alternative, i'd still use .every() though.

manipulating array of json data

I have a project where I have to take some data from a GoogleSheet document and send it to database.
I get the data from googlesheet document, but the problem is how is the data structured.
[
{
class: "A",
name: "Alex",
age: 13
},
{
class: "A",
name: "Mary",
age: 14
},
{
class: "B",
name: "John",
age: 13
},
{
class: "B",
name: "William",
age: 12
}
]
The problem is I want my JSON object to look like this
[
"A": {
{
name: "Alex",
age: 13
},
{
name: "Mary",
age: 13
}
},
"B": {
{
name: "John",
age: 13
},
{
name: "William",
age: 13
}
}
]
Any idea how can I do that? I want to group my objects over an element, like the class. All the people with the same class value to be in the same group/json.
You can make use of Array.reduce and format it.
let data = [{class:"A",name:"Alex",age:13},{class:"A",name:"Mary",age:14},{class:"B",name:"John",age:13},{class:"B",name:"William",age:12}];
//destructuring and renaming it to `_` as `class` is a pre-defined identifier in javascript
const formatData = (data) => data.reduce((res, {
class: _,
...rest
}) => {
//(res[_] || []) --> If the `class` prop is not already present in the res object, then using empty array
//If already present spreading res[_] and adding the current object to the existing array
res[_] = [...(res[_] || []), { ...rest }]
return res;
}, {})
console.log(formatData(data))
.as-console-wrapper {
max-height: 100% !important;
}
You can have a look at Destructuring Assignment for more info about destructuring and assigning new variable names.

How to add specific key values from one object to another array of objects, for each object

What I want to achieve is to add each key value of array b to each object in array a, but not to merge them as I've tried that already and it's no something that it helps, I basically want to add the key and value that I don't have in array a from array b.
array a = [
{
name: "Alex"
},
{
name: "Helen"
},
{
name: "Anna"
}]
array b = [
{
age: "23"
},
{
age: "24"
},
{
age: "25"
}]
What I want to achieve is:
array a = [
{
name: "Alex",
age: "23"
},
{
name: "Helen",
age: "24"
},
{
name: "Anna",
age: "25"
}]
I've tried the options available in here but nothing seems to work. If you guys have any idea, I would really appreciate it.
Use Array.prototype.map():
const a = [{name:"Alex"},{name:"Helen"},{name:"Anna"}],
b = [{age:"23"},{age:"24"},{age:"25"}],
result = a.map(({name},i) => ({name, ...b[i]}))
console.log(result)
.as-console-wrapper{min-height:100%;}

Best way to split a large JSON response into multiple objects and push them to an array

I have a large JSON response that I get back after an HTTP call that looks like this:
0: { index: 0, name: "bob", age: 12, location: "adelaide" ... }
1: { index: 0, name: "jeff", age: 23 ... }
2: { index: 1, name: "sam", age: 25 ... }
...
From this, I want to create an array of objects, with one object for each value of index and its corresponding entries, where I can choose what data goes into those entries. Something like this:
[
{ name: "index_0", index: 0, data: [{ name: "bob", age: 12 }, { name: "jeff", age: 23 }] },
{ name: "index_1", index: 1, data: [{ name: "sam", age: 25 }] },
...
]
I know I need to create objects dynamically here based on the index count, but I'm not sure how to do this.
const newdata = [];
origdata.forEach(function(item){
if(!newdata[item.index])
newdata[item.index] = {name:'index_'+item.index,index:item.index,data:[]};
newdata[item.index].data.push(item);
delete item.index;
});

how to update an array element property with another array element property in underscore.js

First array
userData = [
{ name: abc, age: 24 },
{ name: ghi, age: 22 },
{ name: tyu, age: 20 }
];
Second array
userAge = [
{ age: 25 },
{ age: 26 },
{ age: 22 }
];
Both arrays have the same length.
How do I update the useData[0].age with userAge[0] using Underscore.js?
Since you need to do this over a list of dictionaries, you will need to iterate over the list using _.each.
Something like this will help,
_.each(userData, function(data, index) {
data.age = userAge[index].age;
});
While #martianwars is correct, it could be a little more generic, thus more useful, if it could apply any attribute.
Say we have an array of changes to apply, with each not being necessarily the same attribute or even multiple attributes per object:
var changes = [
{ age: 25 },
{ name: "Guy" },
{ name: "Pierre", age: 22 }
];
Changes could be applied with _.extendOwn
_.each(userData, function(data, index) {
_.extendOwn(data, changes[index]);
});
Proof of concept:
var userData = [{
name: "abc",
age: 24
}, {
name: "ghi",
age: 22
}, {
name: "tyu",
age: 20
}];
var changes = [{
age: 25
}, {
name: "Guy"
}, {
name: "Pierre",
age: 22
}];
_.each(userData, function(data, index) {
_.extendOwn(data, changes[index]);
});
console.log(userData);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
This will only work if the arrays are in sync. Meaning that if there are 4 objects in the userData array, and only 3 changes, they need to be at the right index and that could become a problem.
A solution to this is to have an identification property, often represented by an id attribute.
userData = [
{ id: '1', name: 'abc', age: 24 },
{ id: '2', name: 'ghi', age: 22 },
{ id: '3', name: 'tyu', age: 20 }
];
See Merge 2 objects based on 1 field using Underscore.js for details.

Categories