I expect this is a relatively simple question, but I've been digging around and haven't been able to find a solution as of yet. I am learning MongoDB/NoSQL recently and have a .js file that I'm attempting to run via command line, ex:
mongo < myscript.js
But unfortunately, I am receiving syntax errors (uncaught exception: SyntaxError: unexpected token: identifier :) instead of successful output in the terminal. I am doing some basic .insert(), .update(), and then subsequent .find() and .aggregate() to display that data. Is there some syntax or formatting that I need to adjust when using my code as a script? Running the code in segments inside of the Mongo Shell yields positive results everything runs successfully. Here is my .js code:
db = db.getSiblingDB('PurpleBoxDVD');
db.RentalCollection.insertMany(
[
{
"_id": "0000001",
"userID": "PB0000001",
"userFirstName" : "Drew",
"userLastName": "Weidman",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "What was the name of your first pet?",
"Answer1": "Tim"
},
{
"Question2": "What elementary school did you attend?",
"Answer2": "Desert Hills"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "3",
"movieType": "B",
"title": "True Grit",
"actor":
[
{
"actor1FirstName": "John",
"actor1LastName": "Wayne"
},
{
"actor2FirstName": "Glen",
"actor2LastName": "Campbell"
}
],
"directorFirstName": "Henry",
"directorLastName": "Hathaway",
"genre": ["Adventure","Drama","Western"],
"keyword": ["Rooster Cogburn","US Marshal","Oscar Award Winner"]
},
{
"_id": "0000002",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "The Sons of Katie Elder",
"actor":
[
{
"actor1FirstName": "John",
"actor1LastName": "Wayne"
},
{
"actor2FirstName": "Dean",
"actor2LastName": "Martin"
}
],
"directorFirstName": "Henry",
"directorLastName": "Hathaway",
"genre": ["Western"],
"keyword": ["Gun Fighter","Family"]
},
{
"_id": "0000003",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "The Avengers",
"actor":
[
{
"actor1FirstName": "Robert",
"actor1LastName": "Downey Jr"
},
{
"actor2FirstName": "Chris",
"actor2LastName": "Evans"
}
],
"directorFirstName": "Joss",
"directorLastName": "Whedon",
"genre": ["Adventure","Action","Sci-Fi"],
"keyword": ["Captain America","Iron Man","Thor"]
},
{
"_id": "0000004",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "Greatest Showman",
"actor":
[
{
"actor1FirstName": "Hugh",
"actor1LastName": "Jackman"
},
{
"actor2FirstName": "Michelle",
"actor2LastName": "Williams"
}
],
"directorFirstName": "Michael",
"directorLastName": "Gracey",
"genre": ["Drama","Biography","Musical"],
"keyword": ["Circus","Barnum","Singing","Million Dreams"]
},
{
"_id": "0000005",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "Star Wars: Episode IV - New Hope",
"actor":
[
{
"actor1FirstName": "Mark",
"actor1LastName": "Hamill"
},
{
"actor2FirstName": "Carrie",
"actor2LastName": "Fisher"
}
],
"directorFirstName": "George",
"directorLastName": "Lucas",
"genre": ["Fantasy","Adventure","Action"],
"keyword": ["Jedi Knight","Darth Vader","Yoda"]
},
{
"_id": "0000006",
"userID": "PB0000021",
"userFirstName" : "Spencer",
"userLastName": "Hilton",
"userPhoneNumber": "9999999999",
"userPhoneNumber2": "9999999999",
"userType": "N",
"customerType": "P",
"banStatus": "N",
"password": "CSRocks!",
"question":
[
{
"Question1": "What was your first phone number?",
"Answer1": "9999999999"
},
{
"Question2": "What is your mothers maiden name?",
"Answer2": "Johnson"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "3",
"movieType": "B",
"title": "Deadpool",
"actor":
[
{
"actor1FirstName": "Ryan",
"actor1LastName": "Reynolds"
},
{
"actor2FirstName": "Morena",
"actor2LastName": "Baccarin"
}
],
"directorFirstName": "Tim",
"directorLastName": "Miller",
"genre": ["Adventure","Action","Comedy"],
"keyword": ["Mercinary","Morbid","Healing Power"]
},
{
"_id": "0000007",
"userID": "PB0000027",
"userFirstName" : "Josh",
"userLastName": "Jensen",
"userPhoneNumber": "9999999999",
"userPhoneNumber2": "9999999999",
"userType": "N",
"customerType": "S",
"banStatus": "N",
"password": "AndroidIsKing",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Josh Jensen"
},
{
"Question2": "What was your first phone number?",
"Answer2": "8016429755"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "1",
"movieType": "D",
"title": "X-Men",
"actor":
[
{
"actor1FirstName": "Hugh",
"actor1LastName": "Jackman"
},
{
"actor2FirstName": "Patrick",
"actor2LastName": "Stewart"
}
],
"directorFirstName": "Bryan",
"directorLastName": "Singer",
"genre": ["Adventure","Action","Sci-Fi"],
"keyword": ["Wolverine","Mutants"]
},
{
"_id": "0000008",
"userID": "PB0000101",
"userFirstName" : "Waldo",
"userLastName": "Wildcat",
"userPhoneNumber": "9999999999",
"userPhoneNumber2": "9999999999",
"userType": "N",
"customerType": "S",
"banStatus": "N",
"password": "GoWildcats!",
"question":
[
{
"Question1": "What was the name of your first pet?",
"Answer1": "Thunderbird"
},
{
"Question2": "What elementary school did you attend?",
"Answer2": "Bonneville"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "4",
"movieType": "B",
"title": "The Avengers",
"actor":
[
{
"actor1FirstName": "Robert",
"actor1LastName": "Downey Jr"
},
{
"actor2FirstName": "Chris",
"actor2LastName": "Evans"
}
],
"directorFirstName": "Joss",
"directorLastName": "Whedon",
"genre": ["Adventure","Action","Sci-Fi"],
"keyword": ["Captain America","Iron Man","Thor"]
},
{
"_id": "0000009",
"userID": "PB0000101",
"userFirstName" : "Waldo",
"userLastName": "Wildcat",
"userPhoneNumber": "9999999999",
"userPhoneNumber2": "9999999999",
"userType": "N",
"customerType": "S",
"banStatus": "N",
"password": "GoWildcats!",
"question":
[
{
"Question1": "What was the name of your first pet?",
"Answer1": "Thunderbird"
},
{
"Question2": "What elementary school did you attend?",
"Answer2": "Bonneville"
}
],
"rentalDate": "Jun 28 2020",
"returnDate": "null",
"dueDate": "Jul 01 2020",
"copyNumber": "3",
"movieType": "B",
"title": "Incredibles 2",
"actor":
[
{
"actor1FirstName": "Craig T",
"actor1LastName": "Nelson"
},
{
"actor2FirstName": "Holly",
"actor2LastName": "Hunter"
}
],
"directorFirstName": "Brad",
"directorLastName": "Bird",
"genre": ["Adventure","Action","Animation"],
"keyword": ["Elastigirl","Dash","Jack Jack"]
}
]
);
db.RentalCollection.insertMany(
[
{
"_id": "0000010",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Aug 1 2020",
"returnDate": "null",
"dueDate": "Aug 4 2020",
"copyNumber": "1",
"movieType": "D",
"title": "Deadpool",
"actor":
[
{
"actor1FirstName": "Ryan",
"actor1LastName": "Reynolds"
},
{
"actor2FirstName": "Morena",
"actor2LastName": "Baccarin"
}
],
"directorFirstName": "Tim",
"directorLastName": "Miller",
"genre": ["Adventure","Action","Comedy"],
"keyword": ["Mercinary","Morbid","Healing Power"]
},
{
"_id": "0000011",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Aug 1 2020",
"returnDate": "null",
"dueDate": "Aug 4 2020",
"copyNumber": "1",
"movieType": "D",
"title": "Incredibles 2",
"actor":
[
{
"actor1FirstName": "Craig T",
"actor1LastName": "Nelson"
},
{
"actor2FirstName": "Holly",
"actor2LastName": "Hunter"
}
],
"directorFirstName": "Brad",
"directorLastName": "Bird",
"genre": ["Adventure","Action","Animation"],
"keyword": ["Elastigirl","Dash","Jack Jack"]
},
{
"_id": "0000012",
"userID": "PB0000002",
"userFirstName" : "Kolten",
"userLastName": "Nay",
"userPhoneNumber": "9999999999",
"userType": "A",
"customerType": "P",
"banStatus": "N",
"password": "SQLRules",
"question":
[
{
"Question1": "Who is the best professor in the CS Department?",
"Answer1": "Cody Squadroni"
},
{
"Question2": "What was the name of your first pet?",
"Answer2": "Kirby"
}
],
"rentalDate": "Aug 1 2020",
"returnDate": "null",
"dueDate": "Aug 4 2020",
"copyNumber": "1",
"movieType": "D",
"title": "X-Men",
"actor":
[
{
"actor1FirstName": "Hugh",
"actor1LastName": "Jackman"
},
{
"actor2FirstName": "Patrick",
"actor2LastName": "Stewart"
}
],
"directorFirstName": "Bryan",
"directorLastName": "Singer",
"genre": ["Adventure","Action","Sci-Fi"],
"keyword": ["Wolverine","Mutants"]
},
]
);
db.RentalCollection.find();
db.RentalCollection.updateOne(
{ _id: "0000001"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000002"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000003"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000004"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000005"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000006"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000007"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000008"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000009"},
{ $set: { returnDate: "Jul 01 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000010"},
{ $set: { returnDate: "Aug 02 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000011"},
{ $set: { returnDate: "Aug 02 2020"}}
);
db.RentalCollection.updateOne(
{ _id: "0000012"},
{ $set: { returnDate: "Aug 02 2020"}}
);
db.RentalCollection.aggregate(
{ $project: {_id: 0, userFirstName: 1, userLastName: 1, title: 1, rentalDate: 1, actor1FirstName: 1, actor1LastName: 1,actor2FirstName: 1, actor2LastName: 1, directorFirstName: 1, directorLastName: 1, movieType: 1 }}
);
full error from terminal:
MongoDB shell version v4.2.8
connecting to: mongodb://127.0.0.1:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("775add23-e465-4fe3-a32f-f0ac8a4753cc") }
MongoDB server version: 4.2.8
2020-08-02T10:54:36.056-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell):1:8
2020-08-02T10:54:36.056-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell):1:11
2020-08-02T10:54:36.057-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell):1:8
2020-08-02T10:54:36.057-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell):1:7
2020-08-02T10:54:36.059-0600 E QUERY [js] uncaught exception: SyntaxError: identifier starts immediately after numeric literal :
#(shell):1:8
2020-08-02T10:54:36.059-0600 E QUERY [js] uncaught exception: SyntaxError: expected expression, got '-' :
#(shell):1:2
2020-08-02T10:54:36.059-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell):1:8
2020-08-02T10:54:36.060-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell):1:4
2020-08-02T10:54:36.060-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell):1:13
2020-08-02T10:54:36.061-0600 E QUERY [js] uncaught exception: SyntaxError: unexpected token: identifier :
#(shell):1:3
2020-08-02T10:54:36.061-0600 E QUERY [js] uncaught exception: SyntaxError: expected expression, got '-' :
#(shell):1:2
2020-08-02T10:54:36.061-0600 E QUERY [js] uncaught exception: SyntaxError: identifier starts immediately after numeric literal :
#(shell):1:8
2020-08-02T10:54:36.062-0600 E QUERY [js] uncaught exception: SyntaxError: expected expression, got '>' :
#(shell):1:0
bye
Related
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 1 year ago.
Improve this question
I have the following which is almost where I need to be
var baseData= [{
"id": 1,
"username": "JJ",
"lastLoggedIn": null,
"archive": false,
"forename": "J",
"surname": "J",
"band": "Band 7",
"role": "Developer",
"competency": "Kicks",
"employeeNumber": null,
"name": "Sedation"
}, {
"id": 1,
"username": "JJ",
"lastLoggedIn": null,
"archive": false,
"forename": "J",
"surname": "J",
"band": "Band 7",
"role": "Developer",
"competency": "Kicks",
"employeeNumber": null,
"name": "Safeguarding Level 3"
}, {
"id": 1,
"username": "JJ",
"lastLoggedIn": null,
"archive": false,
"forename": "J",
"surname": "J",
"band": "Band 7",
"role": "Developer",
"competency": "Kicks",
"employeeNumber": null,
"name": "Safeguarding Level 2"
}, {
"id": 276,
"username": "IH",
"lastLoggedIn": null,
"archive": false,
"forename": "I",
"surname": "Hashmi",
"band": "Band 8",
"role": "Chap",
"competency": "Pie",
"employeeNumber": null,
"name": "Safeguarding Level 3"
}, {
"id": 276,
"username": "IH",
"lastLoggedIn": null,
"archive": false,
"forename": "I",
"surname": "Hashmi",
"band": "Band 8",
"role": "Chap",
"competency": "Pie",
"employeeNumber": null,
"name": "Safeguarding Level 2"
}, {
"id": 276,
"username": "IH",
"lastLoggedIn": null,
"archive": false,
"forename": "I",
"surname": "Hashmi",
"band": "Band 8",
"role": "Chap",
"competency": "Pie",
"employeeNumber": null,
"name": "Major Incident"
}, {
"id": 277,
"username": "LC",
"lastLoggedIn": null,
"archive": false,
"forename": "L",
"surname": "C",
"band": "Band 9",
"role": "Lady",
"competency": "Pizza",
"employeeNumber": null,
"name": "WMH ED Nursing"
}, {
"id": 277,
"username": "LC",
"lastLoggedIn": null,
"archive": false,
"forename": "L",
"surname": "C",
"band": "Band 9",
"role": "Lady",
"competency": "Pizza",
"employeeNumber": null,
"name": "Sedation"
}, {
"id": 277,
"username": "LC",
"lastLoggedIn": null,
"archive": false,
"forename": "L",
"surname": "C",
"band": "Band 9",
"role": "Lady",
"competency": "Pizza",
"employeeNumber": null,
"name": "Safeguarding Level 3"
}, {
"id": 281,
"username": "MJ",
"lastLoggedIn": null,
"archive": false,
"forename": "M",
"surname": "J",
"band": "Band 9",
"role": "Solutions Developer Manager",
"competency": "Ninja",
"employeeNumber": "23546329",
"name": "Digital Team"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "WMH ED Medics"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "WMH ED Nursing"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "Sedation"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "Safeguarding Level 3"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "Safeguarding Level 2"
}, {
"id": 283,
"username": "CG",
"lastLoggedIn": null,
"archive": false,
"forename": "C",
"surname": "G",
"band": "5",
"role": "Senior Solutions Developer",
"competency": "LOL",
"employeeNumber": "23546205",
"name": "Major Incident"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "WMH ED Medics"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "WMH ED Nursing"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "Sedation"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "Safeguarding Level 3"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "Safeguarding Level 2"
}, {
"id": 284,
"username": "Bob",
"lastLoggedIn": null,
"archive": true,
"forename": "Bob",
"surname": "Bob",
"band": "Bob",
"role": "Bob",
"competency": "Bob",
"employeeNumber": "XXX",
"name": "Major Incident"
}];
var newData = [];
baseData.forEach(function(item, index) {
if (newData.length === 0) {
newData.push(item);
} else {
var dIndex = -1;
newData.forEach(function(itm, idx) {
if (item.id === itm.id) dIndex = idx;
});
if (dIndex !== -1) {
var oldname = newData[dIndex].name;
if (typeof(oldname).toString() === 'string') {
newData[dIndex].name = [oldname, item.name];
}
} else {
newData.push(item);
}
}
});
console.log(newData);
jsFiddle: https://jsfiddle.net/jeqdbn12/
This groups/reduces the object by the key called "name".
However it seems to nest the values for key, so using the array above, for Bob, I get:
{
archive: true,
band: "Bob",
competency: "Bob",
employeeNumber: "XXX",
forename: "Bob",
id: 284,
lastLoggedIn: null,
name: [[[[["WMH ED Medics", "WMH ED Nursing"], "Sedation"], "Safeguarding Level 3"], "Safeguarding Level 2"], "Major Incident"],
role: "Bob",
surname: "Bob",
username: "Bob"
}
However, what I need is for the key called "name" to be an array of values or a comma delimited string. Any advice?
So I need something like:
...
name: ["value1","value2"]
...
Use Array.reduce
Logic
Loop through array.
Push the nodes to accumulator
Before pushing verify whether there is a node with same id and username already exist in the accumulator.
If yes, push the name of current node to the name array of that node
If not push the node to accumulator with name as a array with the value of current name as the first element.
const baseData = [{"id":1,"username":"JJ","lastLoggedIn":null,"archive":false,"forename":"J","surname":"J","band":"Band 7","role":"Developer","competency":"Kicks","employeeNumber":null,"name":"Sedation"},{"id":1,"username":"JJ","lastLoggedIn":null,"archive":false,"forename":"J","surname":"J","band":"Band 7","role":"Developer","competency":"Kicks","employeeNumber":null,"name":"Safeguarding Level 3"},{"id":1,"username":"JJ","lastLoggedIn":null,"archive":false,"forename":"J","surname":"J","band":"Band 7","role":"Developer","competency":"Kicks","employeeNumber":null,"name":"Safeguarding Level 2"},{"id":276,"username":"IH","lastLoggedIn":null,"archive":false,"forename":"I","surname":"Hashmi","band":"Band 8","role":"Chap","competency":"Pie","employeeNumber":null,"name":"Safeguarding Level 3"},{"id":276,"username":"IH","lastLoggedIn":null,"archive":false,"forename":"I","surname":"Hashmi","band":"Band 8","role":"Chap","competency":"Pie","employeeNumber":null,"name":"Safeguarding Level 2"},{"id":276,"username":"IH","lastLoggedIn":null,"archive":false,"forename":"I","surname":"Hashmi","band":"Band 8","role":"Chap","competency":"Pie","employeeNumber":null,"name":"Major Incident"},{"id":277,"username":"LC","lastLoggedIn":null,"archive":false,"forename":"L","surname":"C","band":"Band 9","role":"Lady","competency":"Pizza","employeeNumber":null,"name":"WMH ED Nursing"},{"id":277,"username":"LC","lastLoggedIn":null,"archive":false,"forename":"L","surname":"C","band":"Band 9","role":"Lady","competency":"Pizza","employeeNumber":null,"name":"Sedation"},{"id":277,"username":"LC","lastLoggedIn":null,"archive":false,"forename":"L","surname":"C","band":"Band 9","role":"Lady","competency":"Pizza","employeeNumber":null,"name":"Safeguarding Level 3"},{"id":281,"username":"MJ","lastLoggedIn":null,"archive":false,"forename":"M","surname":"J","band":"Band 9","role":"Solutions Developer Manager","competency":"Ninja","employeeNumber":"23546329","name":"Digital Team"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"WMH ED Medics"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"WMH ED Nursing"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"Sedation"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"Safeguarding Level 3"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"Safeguarding Level 2"},{"id":283,"username":"CG","lastLoggedIn":null,"archive":false,"forename":"C","surname":"G","band":"5","role":"Senior Solutions Developer","competency":"LOL","employeeNumber":"23546205","name":"Major Incident"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"WMH ED Medics"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"WMH ED Nursing"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"Sedation"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"Safeguarding Level 3"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"Safeguarding Level 2"},{"id":284,"username":"Bob","lastLoggedIn":null,"archive":true,"forename":"Bob","surname":"Bob","band":"Bob","role":"Bob","competency":"Bob","employeeNumber":"XXX","name":"Major Incident"}];
const newData = baseData.reduce((acc, curr) => {
const node = acc.find((item) => item.id === curr.id && item.username === curr.username);
if (node) {
node.name.push(curr.name);
} else {
const newNode = { ...curr };
newNode.name = [curr.name];
acc.push(newNode)
}
return acc;
}, []);
console.log(newData);
I am new to eCharts and I am trying to get a stacked line chart to work. The tooltip shows the data correctly, but there is no y-axis scale and no lines or area fills. If I remove the "stack: 'a'" options from the 'series' section, a non-stacked version of the chart renders correctly. My options and data are as follows:
var myChart = echarts.init(document.getElementById('main'));
var option = {
"animation": false,
"legend": {
"show": true,
"top": "middle",
"left": "right",
"orient": "vertical"
},
"title": {
"text": "Total Raised by Type, Month and Year",
"left": "center"
},
"tooltip": {
"trigger": "axis",
"axisPointer": {
"type": "cross",
"label": {
"backgroundColor": "#6a7985"
}
}
},
"dataset": {
"source": [
[
null,
"Gifts",
"In Kind",
"Pledges"
],
[
"Jul 2017",
"2508",
"1",
"2594"
],
[
"Aug 2017",
"3512",
"2",
"3631"
],
[
"Sep 2017",
"7625",
"4",
"7885"
],
[
"Oct 2017",
"8026",
"4",
"8300"
],
[
"Nov 2017",
"9431",
"5",
"9753"
],
[
"Dec 2017",
"15050",
"8",
"15563"
],
[
"Jan 2018",
"9030",
"5",
"9338"
],
[
"Feb 2018",
"7525",
"4",
"7781"
],
[
"Mar 2018",
"6020",
"3",
"6225"
],
[
"Apr 2018",
"8528",
"4",
"8819"
],
[
"May 2018",
"13043",
"7",
"13488"
],
[
"Jun 2018",
"10033",
"5",
"10375"
],
[
"Jul 2018",
"4311",
"4",
"2159"
],
[
"Aug 2018",
"6036",
"5",
"3022"
],
[
"Sep 2018",
"13106",
"11",
"6563"
],
[
"Oct 2018",
"13796",
"12",
"6908"
],
[
"Nov 2018",
"16210",
"14",
"8117"
],
[
"Dec 2018",
"25867",
"23",
"12953"
],
[
"Jan 2019",
"15520",
"14",
"7772"
],
[
"Feb 2019",
"12934",
"11",
"6477"
],
[
"Mar 2019",
"10347",
"9",
"5181"
],
[
"Apr 2019",
"14658",
"13",
"7340"
],
[
"May 2019",
"22418",
"20",
"11226"
],
[
"Jun 2019",
"17245",
"15",
"8636"
],
[
"Jul 2019",
"1847",
"2",
"1696"
],
[
"Aug 2019",
"2586",
"3",
"2374"
],
[
"Sep 2019",
"5616",
"6",
"5155"
],
[
"Oct 2019",
"5911",
"6",
"5426"
],
[
"Nov 2019",
"6946",
"7",
"6375"
],
[
"Dec 2019",
"11084",
"11",
"10173"
],
[
"Jan 2020",
"6650",
"7",
"6104"
],
[
"Feb 2020",
"5542",
"6",
"5087"
],
[
"Mar 2020",
"4433",
"5",
"4069"
],
[
"Apr 2020",
"6281",
"6",
"5765"
],
[
"May 2020",
"9606",
"10",
"8817"
],
[
"Jun 2020",
"7389",
"8",
"6782"
]
]
},
"xAxis": {
"type": "category",
"axisLabel": {
"rotate": 90
}
},
"yAxis": [
{
"type": "value",
"axisLabel": {
"formatter": "${value}"
}
}
],
"series": [
{
"type": "line",
"areaStyle": {},
"stack": "a"
},
{
"type": "line",
"areaStyle": {},
"stack": "a"
},
{
"type": "line",
"areaStyle": {},
"stack": "a"
}
]
};
myChart.setOption(option);
<script src="//unpkg.com/echarts/dist/echarts.min.js"></script>
<script src="https://underscorejs.org/underscore.js"></script>
<div id="main" style="width: 600px;height:400px;"></div>
I've been able to get every other chart type I need to work as expected except this one. What am I missing?
Don't use null as name for your first dimension but some arbitrary string like 'date':
"source": [
[
"date",
"Gifts",
"In Kind",
"Pledges"
], [...]
Given the following data
[
{
"date": "2017-10-04",
"games": [
{
"notes": "Game was played",
"time": "2017-10-04T20:24:30+00:00",
"sport": "hockey",
"owner": "steve",
"players": "10",
"game_id": 1
},
{
"notes": "Game was played",
"time": "2017-10-04T12:35:30+00:00",
"sport": "lacrosse",
"owner": "steve",
"players": "6",
"game_id": 2
},
{
"notes": "Game was played",
"time": "2017-10-04T10:12:30+00:00",
"sport": "hockey",
"owner": "henry",
"players": "10",
"game_id": 4
}
]
},
{
"date": "2017-10-14",
"games": [
{
"notes": "Game was played",
"time": "2017-10-14T20:32:30+00:00",
"sport": "hockey",
"owner": "steve",
"players": "4",
"game_id": 3
},
{
"notes": "Game was played",
"time": "2017-10-14T20:34:30+00:00",
"sport": "soccer",
"owner": "john",
"players": "12",
"game_id": 5
}
]
}
]
how do I filter out the objects so that I only show the hockey games played on that date. Essentially I need the same array of objects back, but the object should only be shown if the games key = sport: hockey
I know I can only run the filter method on arrays, but I can't figure out how to loop over the object inside of the array and return the whole object again. Any help would be greatly appreciated.
Try:
const filtered = yourArray.map(item => ({...item, games: item.games.filter(game => game.sport === 'hockey')})
// When run, this produces:
[
{
"date": "2017-10-04",
"games": [
{
"game_id": 1,
"notes": "Game was played",
"owner": "steve",
"players": "10",
"sport": "hockey",
"time": "2017-10-04T20:24:30+00:00"
},
{
"game_id": 4,
"notes": "Game was played",
"owner": "henry",
"players": "10",
"sport": "hockey",
"time": "2017-10-04T10:12:30+00:00"
}
]
},
{
"date": "2017-10-14",
"games": [
{
"game_id": 3,
"notes": "Game was played",
"owner": "steve",
"players": "4",
"sport": "hockey",
"time": "2017-10-14T20:32:30+00:00"
}
]
}
]
Which I think is what you want.
I think the following code should do the trick:
var x=[
{
"date": "2017-10-04",
"games": [
{
"notes": "Game was played",
"time": "2017-10-04T20:24:30+00:00",
"sport": "hockey",
"owner": "steve",
"players": "10",
"game_id": 1
},
{
"notes": "Game was played",
"time": "2017-10-04T12:35:30+00:00",
"sport": "lacrosse",
"owner": "steve",
"players": "6",
"game_id": 2
},
{
"notes": "Game was played",
"time": "2017-10-04T10:12:30+00:00",
"sport": "hockey",
"owner": "henry",
"players": "10",
"game_id": 4
}
]
},
{
"date": "2017-10-14",
"games": [
{
"notes": "Game was played",
"time": "2017-10-14T20:32:30+00:00",
"sport": "hockey",
"owner": "steve",
"players": "4",
"game_id": 3
},
{
"notes": "Game was played",
"time": "2017-10-14T20:34:30+00:00",
"sport": "soccer",
"owner": "john",
"players": "12",
"game_id": 5
}
]
}]
function filterByDate (data, date){
return data.filter(function(entry){
return sameDay(new Date(entry.date), date)
})
}
function filterBySport(data, sport){
data.forEach(function(entry){
entry.games=entry.games.filter(function(entry2){
return entry2.sport===sport
})
})
return data
}
function sameDay(date1, date2){ //helper function to check for date equality
return date1.getFullYear()===date2.getFullYear() &&
date1.getMonth()===date2.getMonth() &&
date1.getDay()===date2.getDay()
}
function myFilter(data, date, sportType){ // the final function you have to call
return filterBySport(filterByDate(data, date), sportType)
}
console.log(myFilter(x, new Date("2017-10-04"), "hockey"))
Let's take an array :-
let students = [
{"Name": "Priya","marks": [50, 60, 70, 80, 90]},
{"Name": "Ankita","marks": [80, 90, 95]}
]
Now, I want to filter marks greater than or equal to 90, The code will look like this.
students.map((student) => {
return {...student,
marks: student.marks.filter((mark) => mark >= 90)
}
})
// Result will be [{"Name": "Priya", "marks": [90]}, {"Name": "Ankita","marks": [90,95]}]
Spread operator will expand student and then it will override the marks key with filtered marks value.
If you will not use spread operator,
students.map((student) => student.marks.filter((mark) => mark >= 90))
// Result will be [[90],[90]]
Then you will get only the filtered values and not the values on which you haven't applied the filter.
I am trying to create a simple app to get a City name and display the weather with yahoo weather API. I am able to make a json request and get the answer but I am completely lost on how to retrieve the information from this json.
I can see the response.query , but when I am trying response.query.something I get undefined. Can someone explain to me how I can get the response.query.results.city thing?
Thanks in advance!!
https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22greenland%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys
{
"query": {
"count": 1,
"created": "2015-11-23T16:18:30Z",
"lang": "vi",
"results": {
"channel": {
"title": "Yahoo! Weather - Greenland, GL",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html",
"description": "Yahoo! Weather for Greenland, GL",
"language": "en-us",
"lastBuildDate": "Mon, 23 Nov 2015 12:50 pm CGT",
"ttl": "60",
"location": {
"city": "Greenland",
"country": "Greenland",
"region": ""
},
"units": {
"distance": "mi",
"pressure": "in",
"speed": "mph",
"temperature": "F"
},
"wind": {
"chill": "15",
"direction": "220",
"speed": "15"
},
"atmosphere": {
"humidity": "63",
"pressure": "29.8",
"rising": "0",
"visibility": "6.21"
},
"astronomy": {
"sunrise": "10:45 am",
"sunset": "1:33 pm"
},
"image": {
"title": "Yahoo! Weather",
"width": "142",
"height": "18",
"link": "http://weather.yahoo.com",
"url": "http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif"
},
"item": {
"title": "Conditions for Greenland, GL at 12:50 pm CGT",
"lat": "71.8",
"long": "-42.18",
"link": "http://us.rd.yahoo.com/dailynews/rss/weather/Greenland__GL/*http://weather.yahoo.com/forecast/GLXX0012_f.html",
"pubDate": "Mon, 23 Nov 2015 12:50 pm CGT",
"condition": {
"code": "28",
"date": "Mon, 23 Nov 2015 12:50 pm CGT",
"temp": "27",
"text": "Mostly Cloudy"
},
"description": "\n<img src=\"http://l.yimg.com/a/i/us/we/52/28.gif\"/><br />\n<b>Current Conditions:</b><br />\nMostly Cloudy, 27 F<BR />\n<BR /><b>Forecast:</b><BR />\nMon - AM Clouds/PM Sun. High: 29 Low: 17<br />\nTue - Mostly Cloudy. High: 19 Low: 8<br />\nWed - PM Snow Showers. High: 12 Low: 6<br />\nThu - Mostly Cloudy. High: 10 Low: 0<br />\nFri - Mostly Sunny. High: 1 Low: -8<br />\n<br />\nFull Forecast at Yahoo! Weather<BR/><BR/>\n(provided by <a href=\"http://www.weather.com\" >The Weather Channel</a>)<br/>\n",
"forecast": [
{
"code": "30",
"date": "23 Nov 2015",
"day": "Mon",
"high": "29",
"low": "17",
"text": "AM Clouds/PM Sun"
},
{
"code": "28",
"date": "24 Nov 2015",
"day": "Tue",
"high": "19",
"low": "8",
"text": "Mostly Cloudy"
},
{
"code": "14",
"date": "25 Nov 2015",
"day": "Wed",
"high": "12",
"low": "6",
"text": "PM Snow Showers"
},
{
"code": "28",
"date": "26 Nov 2015",
"day": "Thu",
"high": "10",
"low": "0",
"text": "Mostly Cloudy"
},
{
"code": "34",
"date": "27 Nov 2015",
"day": "Fri",
"high": "1",
"low": "-8",
"text": "Mostly Sunny"
}
],
"guid": {
"isPermaLink": "false",
"content": "GLXX0012_2015_11_27_7_00_CGT"
}
}
}
}
}
}
The response you get is a String, not an Object, so you have to Parse it into an Object using JSON.parse(response).
I think you can try PostMan and JSONView, it will help you to test any API easier.
Hope this can help you.
Just found out what I have to do ( hated js list ).
I used dot notation to get my temps.
So just for the record
var response; // here i have the jsonparse thing
//I want to access something I have to do
response.query.results.channel.wind.speed //and i will get wind's speed
If anyone could use the [] notation it would be great.
Thanks for your time .
I am using d3.js for visualizing the data in collapsible tree layout.Its working fine with flare.json data but it seems to have problems with other json files to work.My json file is valid.
The error I am getting is as follows Uncaught TypeError: Cannot read property 'children' of undefined And my json is here
{
"name":"dummy",
"children":
[
{
"date": "20040309",
"name": "US",
"id": "27",
"kind": "S1",
"application_type": "not found",
"children": [
{"length": 27,
"type": "US",
"id": "28",
"kind": "S",
"name": "Mewdows",
"date": "19730700",
"main_classification": "D 2907"
},
{"length": 27,
"type": "US",
"id": "29",
"kind": "S",
"name": "Askew",
"date": "19731100",
"main_classification": "D 2907"
},
{"length": 27,
"type": "US",
"id": "30",
"kind": "S",
"name": "Adams",
"date": "20020500",
"main_classification": "D 2969"
},
{"length": 27,
"type": "US",
"id": "31",
"kind": "S",
"name": "Adams et al.",
"date": "20020500",
"main_classification": "D 2969"
},
{"length": 27,
"type": "US",
"id": "32",
"kind": "S",
"name": "Adams",
"date": "20020800",
"main_classification": "D 2969"
},
{"length": 27,
"type": "US",
"id": "33",
"kind": "S",
"name": "Adams",
"date": "20030700",
"main_classification": "D 2969"
},
{"length": 27,
"type": "US",
"id": "34",
"kind": "S",
"name": "Robbins",
"date": "20031000",
"main_classification": "D 2969"
}
],
"classification_national": "D 2969",
"length": 7
},
{
"date": "20040309",
"name": "US",
"id": "30997425",
"kind": "S1",
"application_type": "not found",
"children": [
{"length": 7,
"type": "US",
"id": "35",
"kind": "A",
"name": "Alba et al.",
"date": "19851000",
"main_classification": "323308"
},
{"length": 7,
"type": "US",
"id": "36",
"kind": "A",
"name": "Dobberstein",
"date": "19851000",
"main_classification": "323308"
}
],
"classification_national": "D13110",
"length": 2
},
{
"date": "20040309",
"name": "US",
"id": "56992283",
"kind": "B2",
"application_type": "not found",
"children": [
{ "length": 114,
"type": "US",
"id": "151",
"kind": "A",
"name": "Demetre",
"date": "19770300",
"main_classification": "271183"
},
{"length": 114,
"type": "US",
"id": "152",
"kind": "A",
"name": "Carstedt",
"date": "19771100",
"main_classification": "271183"
},
{"length": 114,
"type": "US",
"id": "153",
"kind": "B1",
"name": "Teumer et al.",
"date": "20010100",
"main_classification": "271194"
},
{"length": 114,
"type": "DE",
"id": "154",
"kind": "not found",
"name": "not found",
"date": "19751200",
"main_classification": "not found"
},
{"length": 114,
"type": "DE",
"id": "155",
"kind": "not found",
"name": "not found",
"date": "19770100",
"main_classification": "not found"
},
{"length": 114,
"type": "DE",
"id": "156",
"kind": "not found",
"name": "not found",
"date": "19800300",
"main_classification": "not found"
}
],
"classification_national": "271276",
"length": 6
}
]
}
Any help will be appreciated.