How grupped array of objects on the property in another array? [closed] - javascript

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have a json data. How can I group objects by dates so that the date goes as a new object, and data that has the same date are recorded in persons. Below is the source and desired view
Maybe someone will throw an example, and then I'll finish it myself.
This my source json
[
{
"Info": [
{
"age": "26"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "23"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "30"
}
],
"Date": "2020-08-15"
}
]
This my desired json
[
{
"name": "2020-08-14",
"persons": [
{
"Info": [
{
"age": "26"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "23"
}
],
"Date": "2020-08-14"
}]
},
{
"name": "2020-08-15",
"persons": [
{
"Info": [
{
"age": "30"
}
],
"Date": "2020-08-15"
}
]
}
]
Thank in advance

Using Array#reduce to accumulate the result-data. Iterating over the objects and look if is in the accumulated result object there exists a property with this date. If not create it and add a new object in it with the name-date and a person-property with an empty array. After this add in both cases the hole object to the person array.
At last get the Object#values to get the desired array out of the result-object.
let arr = [
{
"Info": [
{
"age": "26"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "23"
}
],
"Date": "2020-08-14"
},
{
"Info": [
{
"age": "30"
}
],
"Date": "2020-08-15"
}
];
let res = Object.values(arr.reduce((acc, cur) => {
if (acc[cur.Date]=== undefined) {
acc[cur.Date] = {name: cur.Date, persons: []};
}
acc[cur.Date].persons.push(cur);
return acc;
},{}));
console.log(res);

Related

Need to format an object based on another Object data [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Using this Object which is a result from the API ( Columns are dynamic )
let json1 ={
"records": [
{
"ID": "1",
"Value": "A005",
},
{
"ID": "2",
"Value": "A007",
},
{
"ID": "3",
"Value": "B001",
},
{
"ID": "4",
"Value": "B003",
},
],
"conditional":[
{"Value":{'A005':'red','A007':'blue','B001':'green'}},
]
}
I need the below Object to format the data to represent in the UI
let json ={
"records": [
{
"ID": "1",
"Value": "<span style='color;red'>A005</span>",
},
{
"ID": "2",
"Value": "<span style='color;blue'>A007</span>",
},
{
"ID": "3",
"Value": "<span style='color;green'>B001</span>",
},
{
"ID": "4",
"Value": "B003",
},
],
"conditional":[
{"Value":{'A005':'red','A007':'blue','B001':'green'}},
]
}
Data inside conditional and records are dynamic. 'Value' can be any column name, it can also have any number of columns. All data are dynamic
We can do it via Array.forEach()
let json ={
"records": [
{
"ID": "1",
"Value": "A005",
},
{
"ID": "2",
"Value": "A007",
},
{
"ID": "3",
"Value": "B001",
},
{
"ID": "4",
"Value": "B003",
},
],
"conditional":[
{"Value":{'A005':'red','A007':'blue','B001':'green'}},
]
}
// PS: conditional format seems not elegant,especially []
let conditional = json.conditional[0].Value
json.records.forEach(e => {
let color = conditional[e.Value]
e.value = `<span style='color:${color}'>` + e.Value + `</span>`
})
console.log(json)

addToSet mongoDB not adding if one value is repeated

I have the following collection in MongoDB
[
{
"acronym": "front",
"references": [
{
"date": "2020-03-04",
"value": "5.6"
},
{
"date": "2020-03-05",
"value": "6.3"
}
]
}
]
I want to use the function $addToSet in order to add new document into references. I know that it can be done with the following code:
db.collection.update({
"acronym": "front"
},
{
$addToSet: {
"references": {
"date": "2020-03-06",
"value": "6"
}
}
})
And it will add the new document to the array references, so the result is the following:
[
{
"acronym": "front",
"references": [
{
"date": "2020-03-04",
"value": "5.6"
},
{
"date": "2020-03-05",
"value": "6.3"
},
{
"date": "2020-03-06",
"value": "6"
}
]
}
]
QUESTION: What I want to obtain is that in the case of adding a date that is already in the array, the update will no be produced.
Here is the playground: https://mongoplayground.net/p/DPER2RuROEs
Thanks!
You can add another qualifier to the update to prevent duplicated dates
db.collection.update({
"acronym": "front",
"references.date": {
$ne: "2020-03-04"
}
},
{
$addToSet: {
"references": {
"date": "2020-03-04",
"value": "6"
}
}
})
I got the solution from here

How to find the value nested inside an array of objects

Here I have to get value of file_info, I tried doing it using array.includes and array.find(), but got undefined.
My confusion here is related to, under 'facts', the first value is "==", then it has array of values associated to it. I could not figure out to find the values inside that nested object.
I even tried array.find(facts).contains(fileinfo) that did not work as well.
How can I solve this ??
"data": [
{
"task-id": "126e7267",
"type": "A",
"output": {...}
},
{
"task-id": "bdfddff3",
"type": "B",
"output": {
"id": "12b54370",
"facts": [
{
"==": [
"A",
{
"#type": "AA",
"#value": {
"id": "12b54370-4594-4033-a299-5480b593ee6d",
"facts": [
{
"==": [
"time",
1575759643.904254
]
},
{
"==": [
"mime",
"text/plain"
]
},
{
"==": [
"owner",
1000
]
},
{
"==": [
"size",
100
]
},
{
"==": [
"file_info",
"a0s5b2e6e739" // have to find and return this value
]
},
{
"==": [
"time",
{
"#value": "2019-12-07T23:01:50.703Z",
"#type": "timestamp"
}
]
},
],
}
}
]
},
....
]
}
},
{
"task-id": "5f557eac",
"type": "C",
....
},
],
I have tried to validate your json string. It seems to be invalid. For answering this question , i would assume below string to be your json :
{"data":[{"task-id":"126e7267","type":"A","output":{}},{"task-id":"bdfddff3","type":"B","output":{"id":"12b54370","facts":[{"==":["A",{"#type":"AA","#value":{"id":"12b54370-4594-4033-a299-5480b593ee6d","facts":[{"==":[{"time":{"#value":"1575759643.904254"}}]},{"==":["mime","text/plain"]},{"==":["owner",1000]},{"==":["size",100]},{"==":[{"file_info":"a0s5b2e6e739"}]},{"==":["time",{"#value":"2019-12-07T23:01:50.703Z","#type":"timestamp"}]}]}}]}]}},{"task-id":"5f557eac","type":"C"}]}
I had tried to figure out a repetative pattern in your json but since "#value" tag is seen inside only one "facts" object below code should help you getting started . For given json , below code prints the value of "file_info"(Here , i'am assuming that "file_info" should be followed by a colon(:) i.e. "a0s5b2e6e739" is the value you are looking for) :
var jsonStr = '{"data":[{"task-id":"126e7267","type":"A","output":{}},{"task-id":"bdfddff3","type":"B","output":{"id":"12b54370","facts":[{"==":["A",{"#type":"AA","#value":{"id":"12b54370-4594-4033-a299-5480b593ee6d","facts":[{"==":[{"time":{"#value":"1575759643.904254"}}]},{"==":["mime","text/plain"]},{"==":["owner",1000]},{"==":["size",100]},{"==":[{"file_info":"a0s5b2e6e739"}]},{"==":["time",{"#value":"2019-12-07T23:01:50.703Z","#type":"timestamp"}]}]}}]}]}},{"task-id":"5f557eac","type":"C"}]}';
var jsonObj = JSON.parse(jsonStr);
//If there is a repetative pattern , you can replace this hard coding with your pattern.
var objArray = jsonObj["data"][1]["output"]["facts"][0]["=="][1]["#value"]["facts"];
console.log(objArray);
if(objArray && objArray.length >0){
for(let i =0;i<objArray.length;i++){
if(objArray[i] && objArray[i]["=="] && objArray[i]["=="].length > 0 && objArray[i]["=="][0]["file_info"]){
//here "file_info" is fetched
console.log('here ',objArray[i]["=="][0]["file_info"]);
}
}
}
Hope above code helps you to get started.
You can map and filter the object/array to get the result if the format is fixed. Here, I am writing to a Map and retrieving the property I need at the very end.
let data = [
{
"task-id": "126e7267",
"type": "A",
"output": {}
},
{
"task-id": "bdfddff3",
"type": "B",
"output": {
"id": "12b54370",
"facts": [
{
"==": [
"A",
{
"#type": "AA",
"#value": {
"id": "12b54370-4594-4033-a299-5480b593ee6d",
"facts": [
{
"==": [
"time",
1575759643.904254
]
},
{
"==": [
"mime",
"text/plain"
]
},
{
"==": [
"owner",
1000
]
},
{
"==": [
"size",
100
]
},
{
"==": [
"file_info",
"a0s5b2e6e739" // have to find and return this value
]
},
{
"==": [
"time",
{
"#value": "2019-12-07T23:01:50.703Z",
"#type": "timestamp"
}
]
},
],
}
}
]
}
]
}
},
{
"task-id": "5f557eac",
"type": "C",
"output": {}
}
]
const map = new Map()
const facts = data
.map(d => d.output)
.filter(o => o.hasOwnProperty('facts'))
.map(d => d.facts)
.map(i => i[0]["=="][1])
.map(d => d["#value"].facts)
const item = facts.forEach(o => o.forEach(i => map.set(i["=="][0], i["=="][1])))
console.log(map.get("file_info"))

Convert JSON array of objects which have one object each to a JSON array of objects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have an array of objects which has an object within each element and looks like this
[
{
"person": {
"name": "John",
"isActive": true,
"id": 1
}
},
{
"person": {
"name": "Ted",
"isActive": true,
"id": 2
}
}
]
I would like to convert it into this format which doesn't have the class name:
[
{
"Name": "John",
"IsActive": true,
"id": 1
},
{
"Name": "Ted",
"IsActive": true,
"id": 2
}
]
The result will allow me to parse the information into a Kendo UI grid. The conversion is being done in a Typescript file.
You could map the destructured property person.
var data = [{ person: { name: "John", isActive: true, id: 1 } }, { person: { name: "Ted", isActive: true, id: 2 } }],
result = data.map(({ person }) => person);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Use map and return an object with the required key
let data = [{
"person": {
"name": "John",
"isActive": true,
"id": 1
}
},
{
"person": {
"name": "Ted",
"isActive": true,
"id": 2
}
}
];
let newData = data.map(function(item) {
return {
Name: item.person.name,
IsActive: item.person.isActive,
id: item.person.id
};
});
console.log(newData)
Very Simple
people.map(p=>p.person)
var people=[
{
"person": {
"name": "John",
"isActive": true,
"id": 1
}
},
{
"person": {
"name": "Ted",
"isActive": true,
"id": 2
}
}
]
var newPeople=people.map(p=>p.person)
console.log(newPeople)
You can use .map method
const arr = [{"person": {"name": "John","isActive": true,"id": 1}},{"person": {"name": "Ted","isActive": true,"id": 2}}]
.map(({ person }) => {
return {
"Name": person.name,
"IsActive": person.isActive,
"id": person.id
}
})
console.log(arr)

Reformating a JSON tree in node

I'm trying a different approach to my previous question. Basically, I have a JSON object that looks like this:
var data = {
"tree": {
"id": "99842",
"label": "Bill",
"children": [
{
"id": "27878",
"label": "Tom",
"children": []
}
]
},
"index": {
"27878": {
"birthdate": "1/21/1988",
"spouse": "June",
"hometown": "Tulsa, OK"
},
"99842": {
"birthdate": "4/15/1969",
"spouse": "Mary",
"hometown": "Dallas, TX"
}
}
};
As you can see, there are two "top-level" items: a 'tree' object and an 'index' object. I want to parse them together to get this:
{
"rows": [
{
"id": "99842",
"data": [
{
"birthdate": "4/15/1969",
"spouse": "Mary",
"hometown": "Dallas, TX"
}
],
"rows": [
{
"id": "27878",
"data": [
{
"birthdate": "1/21/1988",
"spouse": "June",
"hometown": "Tulsa, OK"
}
],
"rows": []
}
]
}
]
}
It seems like I could do recursion with Q, but it almost seems like overkill and I have a hard time getting my head wrapped around it. I'm thinking through a solution with callbacks but don't quite have it yet. I'd appreciate any help.
Recursion seems perfectly reasonable. Here's one possible solution:
function nestObjects(tree, index) {
var output;
if (tree && index) {
output = {
id: tree.id,
data: index[tree.id],
rows: []
};
if (Array.isArray(tree.children) && tree.children.length) {
for (var i = 0, len = tree.children.length; i < len; i++) {
output.rows.push(nestObjects(tree.children[i], index));
}
}
}
return output;
}
var result = {
rows: [nestObjects(data.tree, data.index)]
};
console.log(result);

Categories