In Javascript, I created a multidimensionnal array, but for another purpose, I need to convert it.
So my array is like this
array [
0 => {
"ulStatic": [
0 => {
"day": "2019-03-30 18:30:00"
"id": "7"
"origin": "intentions"
}
]
"ulDynamic": [
0 => {
"day": "2019-03-30 18:30:00"
"id": "275"
"origin": "obs"
}
]
"ulCreatedDynamic": []
}
1 => {
"ulStatic": [
0 => {
"day": "2019-03-31 09:30:00"
"id": "8"
"origin": "intentions"
}
]
"ulDynamic": []
"ulCreatedDynamic": []
}
2 => {
"ulStatic": []
"ulDynamic": []
"ulCreatedDynamic": [
0 => {
"day": "2019-04-03 19:30:00"
"id": "277"
"origin": "obs"
}
]
}
]
And I'm trying to have this array :
array [
0 => {
"day": "2019-03-30 18:30:00"
"elements": [
0 => {
"id": "7"
"origin": "intentions"
}
1 => {
"id": "275"
"origin": "obs"
}
]
}
1 => {
"day": "2019-03-31 09:30:00"
"elements": [
0 => {
"id": "8"
"origin": "intentions"
}
]
}
2 => {
"day": "2019-04-03 19:30:00"
"elements": [
0 => {
"id": "277"
"origin": "obs"
}
]
}
]
And I must admit that I do not know where to start. I'm looking for map(), splice(), concat(), but it's confusing for me.
Can you help me to give some advices to achieve that ?
Thank you
You could group your data by day by iterating the values of the value of the objects and their array.
var array = [{ ulStatic: [{ day: "2019-03-30 18:30:00", id: "7", origin: "intentions" }], ulDynamic: [{ day: "2019-03-30 18:30:00", id: "275", origin: "obs" }], ulCreatedDynamic: [] }, { ulStatic: [{ day: "2019-03-31 09:30:00", id: "8", origin: "intentions" }], ulDynamic: [], ulCreatedDynamic: [] }, { ulStatic: [], ulDynamic: [], ulCreatedDynamic: [{ day: "2019-04-03 19:30:00", id: "277", origin: "obs" }] }],
result = array.reduce((r, o) => {
Object
.values(o)
.forEach(a => a.forEach(({ day, id, origin }) => {
var temp = r.find(p => day === p.day);
if (!temp) r.push(temp = { day, elements: [] });
temp.elements.push({ id, origin });
}));
return r;
}, []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
let originalArr = [{
ulStatic: [
{
day: '2019-03-30 18:30:00',
id: '7',
origin: 'intentions'
}
],
ulDynamic: [
{
day: '2019-03-30 18:30:00',
id: '275',
origin: 'obs'
}
],
ulCreatedDynamic: []},{ulStatic: [
{
day: '2019-03-31 09:30:00',
id: '8',
origin: 'intentions'
}
],
ulDynamic: [],
ulCreatedDynamic: []},{ ulStatic: [],
ulDynamic: [],
ulCreatedDynamic: [
{
day: '2019-04-03 19:30:00',
id: '277',
origin: 'obs'
}
]}];
let op = originalArr.map(item => {
let ulStatic = item.ulStatic.map(ul => {
return {
id: ul.id,
origin: ul.origin
};
});
let ulDynamic = item.ulDynamic.map(ul => {
return {
id: ul.id,
origin: ul.origin
};
});
let ulCreatedDynamic = item.ulCreatedDynamic.map(ul => {
return {
id: ul.id,
origin: ul.origin
};
});
let day;
if (ulStatic.length > 0) {
day = item.ulStatic[0].day;
} else if (ulDynamic.length > 0) {
day = item.ulDynamic[0].day;
} else if (ulCreatedDynamic.length > 0) {
day = item.ulCreatedDynamic[0].day;
}
return {
day: day,
elements: [].concat(ulStatic).concat(ulDynamic)
};
});
console.log(op);
Check this out
grouping the input by day with reduce and return the object values.
const inputAry = [{
"ulStatic": [{
"day": "2019-03-30 18:30:00",
"id": "7",
"origin": "intentions"
}],
"ulDynamic": [{
"day": "2019-03-30 18:30:00",
"id": "275",
"origin": "obs"
}],
"ulCreatedDynamic": []
},
{
"ulStatic": [{
"day": "2019-03-31 09:30:00",
"id": "8",
"origin": "intentions",
}],
"ulDynamic": [],
"ulCreatedDynamic": []
},
{
"ulStatic": [],
"ulDynamic": [],
"ulCreatedDynamic": [{
"day": "2019-04-03 19:30:00",
"id": "277",
"origin": "obs"
}]
}
];
const groupByDay = inputAry.reduce((group, statics) => {
// flattens the statics array
[].concat.apply([], Object.values(statics))
.forEach(({
day,
id,
origin
}) => {
// creates a dictionary entry with day as key, if already exist use the existing one or creates a new entry
group[day] = group[day] || {
day,
elements: []
};
// push the id and origin to elements
group[day].elements.push({
id,
origin
});
});
return group;
}, {});
const expectedResult = Object.values(groupByDay);
console.log(expectedResult);
Related
Just trying to update the dates in array2 if ID matches in array1 so that they are not null.
let array1 = [{"id":1, "date": "23/11/21"}, {"id":2, "date":"20/11/21"}, {"id":3, "date":"15/11/21"}]
let array2 = [{"id":1, "name": "John", "date": null}, {"id":2, "name": "Max", "date": null}, {"id":3, "name": "Peter", "date": null}]
Desired output:
let array2 = [{"id":1, "name": "John", "date":"23/11/21" }, {"id":2, "name": "Max", "date": "20/11/21"}, {"id":3, "name": "Peter", "date": "15/11/21"}]
How do I use a loop with the indexof() method?
You could use a map method to iterate trough the second array, find an element with the same id in the first array and take the date from there:
let array1 = [{
"id": 1,
"date": "23/11/21"
}, {
"id": 2,
"date": "20/11/21"
}, {
"id": 3,
"date": "22/11/15"
}]
let array2 = [{
"id": 1,
"name": "John",
"date": null
}, {
"id": 2,
"name": "Max",
"date": null
}, {
"id": 3,
"name": "Peter",
"date": null
}];
const updated = array2.map(el => {
const isIdInFirstArr = array1.find(e => e.id === el.id);
if (isIdInFirstArr) {
el.date = isIdInFirstArr.date;
}
return el;
})
console.log(updated)
1) You can efficiently achieve this using Map as:
let array1 = [
{ id: 1, date: "23/11/21" },
{ id: 2, date: "20/11/21" },
{ id: 3, date: "15/11/21" },
];
let array2 = [
{ id: 1, name: "John", date: null },
{ id: 2, name: "Max", date: null },
{ id: 3, name: "Peter", date: null },
];
const map = new Map();
array1.forEach((o) => map.set(o.id, o.date));
let result = array2.map((o) => ({ ...o, date: o.date ?? map.get(o.id) }));
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
2) You can easily achieve the result using map and find
let array1 = [
{ id: 1, date: "23/11/21" },
{ id: 2, date: "20/11/21" },
{ id: 3, date: "15/11/21" },
];
let array2 = [
{ id: 1, name: "John", date: null },
{ id: 2, name: "Max", date: null },
{ id: 3, name: "Peter", date: null },
];
let result = array2.map(o => ({ ...o, date: o.date ?? array1.find(obj => obj.id === o.id)?.date}));
console.log(result)
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }
I am trying to get the value of "type" from the object by iterating over it. The object looks like this.
{
"team": {
"table": [
{
"cityCode": 123,
"list": {
"players": [
{
"name": "peter",
"school": "x",
"awards": {
"type": "gold"
},
"year": 2019
}
]
}
},
{
"cityCode": 456,
"list": {
"players": [
{
"name": "Dave",
"school": "y",
"awards": {
"type": "silver"
},
"year": 2018
}
]
}
}
]
}
}
I am able to get the type values using this:
const table = team.table;
for (let i = 0; i < table.length; i++) {
const values = {
type: table[i].list.players
.filter((a) => a.awards != null)
.map((a) => a.awards.type)
.join(" "),
};
}
However, I want to use another filter on the "list" to filter non null lists. So how can I achieve that.
You want to check Check if 'list' key exists inside a team.table JSON object
you can check by
if(table[i].hasOwnProperty('list')){
}
code is
const table = team.table;
for (let i = 0; i < table.length; i++) {
if(table[i].hasOwnProperty('list')){
const values = {
type: table[i].list.players
.filter((a) => a.awards != null)
.map((a) => a.awards.type)
.join(" "),
};
}
}
1) You can get all type using flatMap and map as:
obj.team.table.flatMap((o) => o.list.players.map((o) => o.awards.type))
const obj = {
team: {
table: [
{
cityCode: 123,
list: {
players: [
{
name: "peter",
school: "x",
awards: {
type: "gold",
},
year: 2019,
},
],
},
},
{
cityCode: 456,
list: {
players: [
{
name: "Dave",
school: "y",
awards: {
type: "silver",
},
year: 2018,
},
],
},
},
],
},
};
const types = obj.team.table.flatMap((o) => o.list.players.map((o) => o.awards.type));
console.log(types);
2) Using forEach and destructuring as:
const obj = {
team: {
table: [
{
cityCode: 123,
list: {
players: [
{
name: "peter",
school: "x",
awards: {
type: "gold",
},
year: 2019,
},
],
},
},
{
cityCode: 456,
list: {
players: [
{
name: "Dave",
school: "y",
awards: {
type: "silver",
},
year: 2018,
},
],
},
},
],
},
};
const table = obj.team.table;
const types = [];
for (let i = 0; i < table.length; i++) {
const { list: { players } } = table[i]
players.forEach(({ awards: { type }}) => types.push(type))
}
console.log(types);
It will be cleaner to use forEach.
You will need 2 forEach due to your data structure.
But below code will:
check if awards is null
check if awards.type is null
const data = {
"team": {
"table": [
{
"cityCode": 123,
"list": {
"players": [
{
"name": "peter",
"school": "x",
"awards": {
"type": "gold"
},
"year": 2019
}
]
}
},
{
"cityCode": 456,
"list": {
"players": [
{
"name": "Dave",
"school": "y",
"awards": {
"type": "silver"
},
"year": 2018
},
{
"name": "Dave",
"school": "y",
"awards": {
"type": "gold"
},
"year": 2016
}
]
}
},
{
"cityCode": 444,
"list": {
"players": [
{
"name": "James",
"school": "y",
"awards": {
"type": null
},
"year": 2016
}
]
}
},
{
"cityCode": 555,
"list": {
"players": [
{
"name": "Name 101",
"school": "y",
"awards": {
"type": "platinum"
},
"year": 2016
},
{
"name": "Name 102",
"school": "y",
"awards": {
"type": null
},
"year": 2016
},
{
"name": "Name 103",
"school": "y",
"awards": null,
"year": 2016
},
]
}
}
]
}
}
// Expanded your data with more items
const data1 = data.team.table;
let types = []
data1.forEach((item, index) => {
item.list.players.forEach((player) => {
const awards = player.awards;
if (awards !== null && awards.type !== null) {
types = [...types, awards.type];
}
})
})
// Get the list of types
console.log(types);
// Get unique list of types
let unique_types = [...new Set(types)]
console.log(unique_types);
I am trying to convert object with object inside to array of objects.
I have the data as:
"data" :{
"tDetails": {
"tName": "Limited",
"tPay": " xyz",
},
"bDetails": {
"bName": "name",
"bid": "bid",
"bNo": "123456",
},
"iDetails": {
"iName": "iname",
"iBranch": "ibranch",
},
}
i want to convert it to array of objects with each iteration over the three headers(tDetails, iDetails,bDetails) as:
const newData = [
{
"id": 1,
"title": "tDetails",
"content": [
{
"id": 1,
"key": "tName",
"value": "Limited"
},
{
"id": 2,
"key": "tPay",
"value": "xyz"
}
]
},
{
"id": 2,
"title": "bDetails",
"content": [
{
"id": 1,
"key": "bId",
"value": "12345"
}
]
},
{
"id": 3,
"title": "iDetails",
"content": [
{
"id": 1,
"key": "iName",
"value": "iname"
},{
"id":2,
"key": "iBranch",
"value": "ibranch"
}
]
},
]
const NewData = () => {
let newDetails = [];
let newHeader = '';
for (const header in data) {
// header here is 'tDetails', bDetails, iDetails
const headerData = data[header];
newDetails = Object.entries(headerData).map(([key, value]) => ({
key,
value,
}));
newHeader = header;
}
return [
{
title: newHeader,
content: newDetails,
},
];
};
This returns me the data of only the last part(iDetails) as it is returned after the for loop.What should be changed here
Map the object's entries instead of using for..in, and in the callback, return the transformed object.
const data={tDetails:{tName:"Limited",tPay:" xyz"},bDetails:{bName:"name",bid:"bid",bNo:"123456"},iDetails:{iName:"iname",iBranch:"ibranch"}};
const newData = Object.entries(data).map(([title, obj], i) => ({
title,
id: i + 1,
content: Object.entries(obj).map(([key, value], j) => ({
id: j + 1,
key,
value
}))
}));
console.log(newData);
I would like to obtain the value of name using the code below.
When find(z => z.key.value === 123) be added to the code ,there will be this error :
x.obj.array_1.find(...).find is not a function
What's wrong with this code :
const data = [{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "123"
}
}]
}]
}]
},
"name": "obj1"
},
{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "456"
}
}]
}]
}]
},
"name": "obj2"
},
{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "789"
}
}]
}]
}]
},
"name": "obj3"
}
]
const name = data
.filter(x =>
x.obj.array_1.find(y => y.array_2).find(y => y.array_3).find(z => z.key.value === 123)
).map(x => x.name);
console.log(name)
Small fix here, this work provided that your real life data is also in this shape
After finding the propriate array_1 element that have array2[0] which has non-nil array_3, you have to access that array_3 to iterate and find the expected value
Edited (because array_2 and array_3 may have multiple elements): you have to extract all array_3 element (using flatMap to flatten the nested array) and do the condition check from that extracted array
const name = data
.filter((x) => {
const flattened_array_3 = x.obj.array_1.flatMap((array_1_el) =>
array_1_el.array_2.flatMap((array_2_el) => array_2_el.array_3)
)
return flattened_array_3.find((array_3_el) => +array_3_el.key.value === 123)
})
.map((x) => x.name)
Full code
const data = [
{
obj: {
array_1: [
{
array_2: [
{
array_3: [
{
key: {
value: "123",
},
},
],
},
],
},
],
},
name: "obj1",
},
{
obj: {
array_1: [
{
array_2: [
{
array_3: [
{
key: {
value: "456",
},
},
],
},
],
},
],
},
name: "obj2",
},
{
obj: {
array_1: [
{
array_2: [
{
array_3: [
{
key: {
value: "789",
},
},
],
},
],
},
],
},
name: "obj3",
},
]
const name = data
.filter((x) => {
const flattened_array_3 = x.obj.array_1.flatMap((array_1_el) =>
array_1_el.array_2.flatMap((array_2_el) => array_2_el.array_3)
)
return flattened_array_3.find((array_3_el) => +array_3_el.key.value === 123)
})
.map((x) => x.name)
console.log(name)
Reference: flatMap
x.obj.array_1.find(y => y.array_2)
is undefined.
Try to split your code
In order to work on this structure, I advise you to change the way you represent data.
It seems like some kind of output of tokenization. There have to bee tools to extract information from the path for example XPath, etc.
On the other hand below code should work for your case
The key point is using flatMap to rest inner arrays to upper one
const data = [{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "123"
}
}]
}]
}]
},
"name": "obj1"
},
{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "456"
}
}]
}]
}]
},
"name": "obj2"
},
{
"obj": {
"array_1": [{
"array_2": [{
"array_3": [{
"key": {
"value": "789"
}
}]
}]
}]
},
"name": "obj3"
}
]
var obj = data.find(x=> x.obj.array_1.flatMap(x=>x.array_2).flatMap(x=>x.array_3)[0].key.value==789)
console.log(obj.name)
I have the following array of objects
{
"nome": "jose",
"categoria": [
{ "id": "1" },
{ "id": "3" },
]
},
{
"nome": "maria",
"categoria": [
{ "id": "2" },
]
},
{
"nome": "pedro",
"categoria": [
{ "id": "1" },
]
}
I have to reorder in another array of categories. Something like this:
{
"id": "1",
"pessoas": [
{
"nome": "jose",
"categoria": [
{ "id": "1" },
{ "id": "3" },
]
},
{
"nome": "pedro",
"categoria": [
{ "id": "1" },
]
},
]
},
{
"id": "2",
"pessoas": [
{
"nome": "maria",
"categoria": [
{ "id": "2" }
]
},
]
},
I have try with the function reduce(), but I couldn't because it is not an object, but a array of objects (categoria)
const group = data.reduce((r, array) => {
r[array.categoria.id] = [...r[array.categoria.id] || [], array];
return r;
}, {});
Someone can help me please?
You could take an object for grouping by id. Inside of reduce, categoria is iterated as well for getting the needed id.
var data = [{ nome: "jose", categoria: [{ id: "1" }, { id: "3" }] }, { nome: "maria", categoria: [{ id: "2" }] }, { nome: "pedro", categoria: [{ id: "1" }] }],
result = Object.values(data.reduce((r, o) => {
o.categoria.forEach(({ id }) => {
if (!r[id]) r[id] = { id, pessoas: [] };
r[id].pessoas.push(o);
});
return r;
}, {}));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }