I have an array of objects, which has an array of 'tracks.' What I would like to do is compare the mbid values across all of the track objects, check if there are any duplicates, and store the duplicate track objects into a new array. Any help or guidance?
[
{
"track": [
{
"name": "Radiapathy",
"mbid": "4c0767f1-1c2e-4790-a8d1-ee7f78f0ac84",
"url": "http://www.last.fm/music/The+Velvet+Teen/_/Radiapathy"
},
{
"name": "How Did I Get Here",
"mbid": "64b3078f-89cd-4ad5-bc7a-b43af082b00f",
"url": "http://www.last.fm/music/Odesza/_/How+Did+I+Get+Here"
},
{
"name": "Sunshine Roof",
"mbid": "837db975-c93e-45ca-992c-0c924ef0f34f",
"url": "http://www.last.fm/music/The+Innocence+Mission/_/Sunshine+Roof"
}
]
},
{
"track": [
{
"name": "Traveling",
"mbid": "b40c24b8-3295-4219-af59-855b69958ca2",
"url": "http://www.last.fm/music/Tennis/_/Traveling"
},
{
"name": "Ghost",
"mbid": "6273ae8f-3d2c-44c6-8c0d-53013ba79b4e",
"url": "http://www.last.fm/music/Neutral+Milk+Hotel/_/Ghost"
},
{
"name": "Strange",
"mbid": "5a015df2-6c4a-4192-bea8-14ec5f297713",
"url": "http://www.last.fm/music/Built+to+Spill/_/Strange"
}
]
},
{
"track": [
{
"name": "Radiapathy",
"mbid": "4c0767f1-1c2e-4790-a8d1-ee7f78f0ac84",
"url": "http://www.last.fm/music/The+Velvet+Teen/_/Radiapathy"
},
{
"name": "Let Me Show You Love",
"mbid": "",
"url": "http://www.last.fm/music/Cut+Copy/_/Let+Me+Show+You+Love"
},
{
"name": "Footsteps",
"mbid": "",
"url": "http://www.last.fm/music/Cut+Copy/_/Footsteps"
}
]
}
]
The answer to this question (Elminating duplicates in a JSON object) pretty much answers yours. I've used this (the "Smart(er) way") and modified to suit your array & requirements:
var key = null;
var noDupes = [];
var dupes = [];
for (var i = 0; i < arr.length; i++)
{
// loop through each track:
for (var j = 0; j < arr[i].track.length; j++)
{
key = arr[i].track[j].mbid;
if (!hash.contains(key))
{
hash.add(key);
noDupes.push(arr[i].track[j]); // if not duplicate
}
else
{
dupes.push(arr[i].track[j]); // if duplicate
}
}
}
http://jsfiddle.net/6wspy/
Related
I create a Pokédex app, but i ran in some Problems with the double type Pokémon:
I call pokeapi twice to 2 endpoints (one for each Type), and i need to compare the Results in different Ways.
let a = {
"double_damage_from": [
{
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/"
},
{
"name": "rock",
"url": "https://pokeapi.co/api/v2/type/6/"
},
{
"name": "water",
"url": "https://pokeapi.co/api/v2/type/11/"
}
],
"half_damage_from": [
{
"name": "bug",
"url": "https://pokeapi.co/api/v2/type/7/"
},
{
"name": "steel",
"url": "https://pokeapi.co/api/v2/type/9/"
},
{
"name": "fire",
"url": "https://pokeapi.co/api/v2/type/10/"
},
{
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/"
},
{
"name": "ice",
"url": "https://pokeapi.co/api/v2/type/15/"
},
{
"name": "fairy",
"url": "https://pokeapi.co/api/v2/type/18/"
}
],
"no_damage_from": []
};
let b = {
"double_damage_from": [
{
"name": "fighting",
"url": "https://pokeapi.co/api/v2/type/2/"
},
{
"name": "ground",
"url": "https://pokeapi.co/api/v2/type/5/"
},
{
"name": "steel",
"url": "https://pokeapi.co/api/v2/type/9/"
},
{
"name": "water",
"url": "https://pokeapi.co/api/v2/type/11/"
},
{
"name": "grass",
"url": "https://pokeapi.co/api/v2/type/12/"
}
],
"half_damage_from": [
{
"name": "normal",
"url": "https://pokeapi.co/api/v2/type/1/"
},
{
"name": "flying",
"url": "https://pokeapi.co/api/v2/type/3/"
},
{
"name": "poison",
"url": "https://pokeapi.co/api/v2/type/4/"
},
{
"name": "fire",
"url": "https://pokeapi.co/api/v2/type/10/"
}
],
"no_damage_from": []
};
I need to Compare the Data and get the Matches in a array.
This works fine and i got the 4x, 1x, and 1/4x damage in a array:
getMatch(a, b) {
let matches = [];
for (let i = 0; i < a.length; i++) {
for (let e = 0; e < b.length; e++) {
if (a[i].name === b[e].name) matches.push(a[i]);
}
}
return matches;
}
compareTypes(a, b) {
let four_damage_from = this.getMatch(a.double_damage_from, b.double_damage_from);
let double_damage_from = [];
let normal_damage_from = this.getMatch(a.double_damage_from, b.half_damage_from);
let half_damage_from = [];
let quarter_damage_from = this.getMatch(a.half_damage_from, b.half_damage_from);
let no_damage_from = this.getMatch(a.no_damage_from, b.no_damage_from);
let matches = { four_damage_from, double_damage_from, normal_damage_from, half_damage_from, quarter_damage_from, no_damage_from };
return matches;
}
to find the correct types for double_damage_from i have to merge a.double_damage_from and b.double_damage_from e.g. to c.double_damage_from. then i have to remove from c.double_damage_from all types that are in four_damage_from, normal_damage_from, quarter_damage_from, no_damage_from to get the correct 2x types, the same with half_damage_from.
I tried many solution but i didn't figure out how to solve this.
greetings Raphi
It Works with lodash, i need to think a bit about but basically works.
removeMatch(union, remove, double = false) {
union = _.differenceBy(union, remove.four_damage_from, 'name');
union = _.differenceBy(union, remove.normal_damage_from, 'name');
union = _.differenceBy(union, remove.quarter_damage_from, 'name');
// union = _.differenceBy(union, remove.no_damage_from, 'name');
if(double) {
union = _.differenceBy(union, remove.half_damage_from, 'name');
} else {
union = _.differenceBy(union, remove.double_damage_from, 'name');
}
return union;
}
I am having an json object below. I want to delete a part if the question value is empty. So according to below json I need to delete the id=7602 portion.
[ {
"id": 9333,
"component": "question_pool",
"sub_comp_arr": [
{
"id": 7769,
"component": "question",
"sub_comp_arr": [
{
"id": 2552,
"component": "question_segment",
"value": "Answer1"
},
{
"id": 1011,
"component": "question_segment",
"value": "Answer2"
},
{
"id": 8691,
"component": "question_segment",
"value": "Answer3"
}
],
"type": "single_choice",
"value": "<p>Question1?</p>\n"
},
{
"id": 7602,
"component": "question",
"sub_comp_arr": [
{
"id": 921,
"component": "question_segment",
"value": ""
}
],
"type": "single_choice",
"value": ""
}
]
},{...}
]
I have implemet the code as below
var y= content_json.content_arr;
var keyCount = Object.keys(y).length;
for (var i = 0; i < keyCount; i++) {
var questionCount = (content_json.content_arr[i]['sub_comp_arr']).length;
for (let j = 0; j < questionCount; j++){
var emptyquestion= ((content_json.content_arr[i]['sub_comp_arr'][j]['value']).trim()).length;
if (emptyquestion===0){
delete (content_json.content_arr[i]['sub_comp_arr'][j]);
}
}
}
But the problem is if I use delete (content_json.content_arr[i]['sub_comp_arr'][j]); It is saving a null value on my Json, Which I don't want. How to achieve it
You could use filter instead.
content_json.content_arr[i].sub_comp_arr = content_json.content_arr[i].sub_comp_arr.filter(q => q.value)
I have two array of objects namely:-
Modules
Learners Answered Questions (laq)
In my Modules objects I have a property named quiz which has an array of questions. I want to compare this array with the Learners Answered Questions (laq), an array of nested arrays. If the Learners Answered Questions (laq) has an array that matches with the array of questions in one of the Module.quiz among the array of Modules then I want to attach it an attribute of the Learners Answered Questions(laq). The question in array of questions of the Modules object and the question in the Learners Answered Questions Array of Arrays has a common property of text. Using this I want to compare if both the arrays are equal and if equal then attach the Array from the Learners Answered Questions Arrays as a property to the Modules object.
For example consider:-
Modules = [module_1, module_2, module_3]
module1.quiz.questions = [question_1, question_2, question_3]
module2.quiz.questions = [question_4, question_5, question_6]
module3.quiz.questions = [question_7, question_8, question_9]
Learners_Answered_QUestions = [
[question_7, question_8, question_9],
[question_1, question_2, question_3],
[question_4, question_5, question_6]
]
I cannot compare these arrays directly since they have different properties. So I have to individually check if all the Questions from an Array of one of the Arrays of Learner_Answered_Questions are having same question in one of the Modules Quiz property.
Here is my code:-
for(var i = 0; i < $scope.modules.length; i++){
for(var j = 0; j < $scope.laq.length; j++){
if($scope.modules[i].quiz[0].questions.length === $scope.laq[j].length){
var array_size = $scope.laq[j].length;
for(var k = 0; k < array_size; k++){
if($scope.modules[i].quiz[0].questions[k].text === $scope.laq[j][k].quiz_question.text){
$scope.attach = true;
}
else{
$scope.attach = false;
};
};
};
if($scope.attach === true){
console.log($scope.attach);
$scope.modules[i].lq_exists = true;
$scope.modules[i].learner_quiz = $scope.laq[j];
console.log($scope.modules[i]);
}
};
};
My code is not working as well as I want to make it more reliable even if the order of the questions in any of the array is changed.
Here is the sample json format:-
module1.quiz = {
"url": "http://localhost:8080/api/registration_quiz/38/",
"id": 38,
"quiz_name": "Rakesh BIO",
"module_referred": "http://localhost:8080/api/registration_courses_modules/98/",
"questions": [
{
"url": "http://localhost:8080/api/registration_quiz_questions/109/",
"quiz": "http://localhost:8080/api/registration_quiz/38/",
"q_type": "MCQ",
"text": "What is Rakesh's profession",
"possible_answers": [
{
"url": "http://localhost:8080/api/registration_answer_options/306/",
"text": "cook"
},
{
"url": "http://localhost:8080/api/registration_answer_options/307/",
"text": "Accountant"
},
{
"url": "http://localhost:8080/api/registration_answer_options/308/",
"text": "IT"
},
{
"url": "http://localhost:8080/api/registration_answer_options/309/",
"text": "Plumber"
}
],
"selected": null,
"correct": {
"url": "http://localhost:8080/api/registration_answer_options/308/",
"text": "IT"
}
},
{
"url": "http://localhost:8080/api/registration_quiz_questions/110/",
"quiz": "http://localhost:8080/api/registration_quiz/38/",
"q_type": "MCQ",
"text": "What is his place's name?",
"possible_answers": [
{
"url": "http://localhost:8080/api/registration_answer_options/310/",
"text": "Yavatmal"
},
{
"url": "http://localhost:8080/api/registration_answer_options/311/",
"text": "Dhule"
},
{
"url": "http://localhost:8080/api/registration_answer_options/312/",
"text": "Sangamner"
},
{
"url": "http://localhost:8080/api/registration_answer_options/313/",
"text": "Solapur"
}
],
"selected": null,
"correct": {
"url": "http://localhost:8080/api/registration_answer_options/311/",
"text": "Dhule"
}
}
]
}
and the Learners_answered_questions JSON:-
lqa = [
{
"quiz_question": {
"url": "http://localhost:8080/api/registration_quiz_questions/110/",
"quiz": "http://localhost:8080/api/registration_quiz/38/",
"q_type": "MCQ",
"text": "What is his place's name?",
"possible_answers": [
{
"url": "http://localhost:8080/api/registration_answer_options/310/",
"text": "Yavatmal"
},
{
"url": "http://localhost:8080/api/registration_answer_options/311/",
"text": "Dhule"
},
{
"url": "http://localhost:8080/api/registration_answer_options/312/",
"text": "Sangamner"
},
{
"url": "http://localhost:8080/api/registration_answer_options/313/",
"text": "Solapur"
}
],
"selected": null,
"correct": {
"url": "http://localhost:8080/api/registration_answer_options/311/",
"text": "Dhule"
}
},
"learner": {
"url": "http://localhost:8080/api/registration_learners/4/",
"user": "http://localhost:8080/api/registration_custom_users/4/",
"profile_picture": null,
"courses_learning": "Django"
},
"chosen_option": {
"url": "http://localhost:8080/api/registration_answer_options/250/",
"text": "No Answer Selected"
}
},
{
"quiz_question": {
"url": "http://localhost:8080/api/registration_quiz_questions/109/",
"quiz": "http://localhost:8080/api/registration_quiz/38/",
"q_type": "MCQ",
"text": "What is Rakesh's profession",
"possible_answers": [
{
"url": "http://localhost:8080/api/registration_answer_options/306/",
"text": "cook"
},
{
"url": "http://localhost:8080/api/registration_answer_options/307/",
"text": "Accountant"
},
{
"url": "http://localhost:8080/api/registration_answer_options/308/",
"text": "IT"
},
{
"url": "http://localhost:8080/api/registration_answer_options/309/",
"text": "Plumber"
}
],
"selected": null,
"correct": {
"url": "http://localhost:8080/api/registration_answer_options/308/",
"text": "IT"
}
},
"learner": {
"url": "http://localhost:8080/api/registration_learners/4/",
"user": "http://localhost:8080/api/registration_custom_users/4/",
"profile_picture": null,
"courses_learning": "Django"
},
"chosen_option": {
"url": "http://localhost:8080/api/registration_answer_options/306/",
"text": "cook"
}
}
]
For the purpose of this question, I've simplified the JSON's to only what's relevant.
Find a fully working algorithm below (you can play around and change the lengths or texts to check all the cases)
var modules = [
{
"id": 38,
"questions": [
{
"text": "What is Rakesh's profession"
},
{
"text": "What is his place's name?"
}
]
},
{
"id": 39,
"questions": [
{
"text": "What is Protozoid's profession"
},
{
"text": "What is his car's name?"
}
]
}
];
var laq = [
[{
"quiz_question": {
"text": "What is his place's name?"
}
},
{
"quiz_question": {
"text": "What is Rakesh's profession"
}
}],
[{
"quiz_question": {
"text": "What is Protozoid's profession"
}
},
{
"quiz_question": {
"text": "This shouldnt be attached because not all questions match"
}
}]
];
modules.forEach(function (module) {
var shouldAttach = true;
var lqReference = [];
laq.forEach(function (laqItem) {
var matchedCount = 0;
module.questions.forEach(function (moduleQuestion) {
laqItem.forEach(function (laqItemQuestion) {
// console.log(moduleQuestion.text, laqItemQuestion.quiz_question.text, moduleQuestion.text == laqItemQuestion.quiz_question.text);
if (moduleQuestion.text == laqItemQuestion.quiz_question.text) {
matchedCount++;
}
});
});
// console.log("should attach1", matchedCount, laqItem.length);
if (matchedCount == laqItem.length) {
shouldAttach = true;
lqReference = laqItem;
} else {
matchedCount = 0;
}
// console.log("should attach2", shouldAttach);
// If questions matched but different lengths
if (shouldAttach && module.questions.length !== lqReference.length) {
shouldAttach = false;
}
});
// console.log("should attach3", shouldAttach);
if (shouldAttach) {
module.lq_exists = true;
module.learner_quiz = lqReference;
}
});
console.log(modules);
Here is my JSON array code
var App = [
{
"id": "123",
"caption": "Test",
"description": "Test Desc"
},
{
"id": "345",
"caption": "adsasdasd",
"description": ""
},
{
"id": "456",
"caption": "adsasdasd",
"description": ""
},
{
"id": "578",
"caption": "adsasdasd",
"description": ""
}
]
i tried with the following code
var obj = $.parseJSON(App);
alert(JSON.stringify(obj,4,null));
var AppLen = obj[i].length;
alert(AppLen);
but i didn't get any solution. Let me know if i missed any thing to get the JSON object array length.
your data is already json format:do like this
var App = [
{
"id": "123",
"caption": "Test",
"description": "Test Desc"
},
{
"id": "345",
"caption": "adsasdasd",
"description": ""
},
{
"id": "456",
"caption": "adsasdasd",
"description": ""
},
{
"id": "578",
"caption": "adsasdasd",
"description": ""
}
];
console.log(App);
console.log(App[0].length);// you can not get length from this because it is not array it's an object now.
var AppLen = App.length;
alert(AppLen);
obj.size() or obj.length
If that dont run try this: Length of a JavaScript object
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
// Get the size of an object
var size = Object.size(myArray);
From the below JSON, how can I retrieve title from the note and notes using a for loop and ajax to retrieve?
{
"infos": {
"info": [
{
"startYear": "1900",
"endYear": "1930",
"timeZoneDesc": "daweerrewereopreproewropewredfkfdufssfsfsfsfrerewrBlahhhhh..",
"timeZoneID": "1",
"note": {
"notes": [
{
"id": "1",
"title": "Mmm"
},
{
"id": "2",
"title": "Wmm"
},
{
"id": "3",
"title": "Smm"
}
]
},
"links": [
{ "id": "1", "title": "Red House", "url": "http://infopedia.nl.sg/articles/SIP_611_2004-12-24.html" },
{ "id": "2", "title": "Joo Chiat", "url": "http://www.the-inncrowd.com/joochiat.htm" },
{ "id": "3", "title": "Bake", "url": "https://thelongnwindingroad.wordpress.com/tag/red-house-bakery" }
]
}
I tried out the code below but it doesn't work - it either says:
is null
not an object
length is null
r not an object
var detail = eval(xmlhttprequest.responseText)
var rss = detail.infos.info
for(var i = 0; i<rss.length; i++)
startyear += rss[i].startyear
Use
for (i = 0; i < 3; i++) {
alert(JSON.infos.info[0].note.notes[i].title);
}
TRY IT HERE: JSFIDDLE WORKING EXAMPLE
BTW your JSON is not valid. Use this JSON:
var JSON = {
"infos": {
"info": [
{
"startYear": "1900",
"endYear": "1930",
"timeZoneDesc": "daweerrewereopreproewropewredfkfdufssfsfsfsfrerewrBlahhhhh..",
"timeZoneID": "1",
"note": {
"notes": [
{
"id": "1",
"title": "Mmm"
},
{
"id": "2",
"title": "Wmm"
},
{
"id": "3",
"title": "Smm"
}
]
},
"links": [
{
"id": "1",
"title": "Red House",
"url": "http://infopedia.nl.sg/articles/SIP_611_2004-12-24.html"
},
{
"id": "2",
"title": "Joo Chiat",
"url": "http://www.the-inncrowd.com/joochiat.htm"
},
{
"id": "3",
"title": "Bake",
"url": "https://thelongnwindingroad.wordpress.com/tag/red-house-bakery"
}
]
}
]
}
}
EDIT:
Here is what you want:
var infoLength= JSON.infos.info.length;
for (infoIndex = 0; infoIndex < infoLength; infoIndex++) {
var notesLength= JSON.infos.info[infoIndex].note.notes.length;
for (noteIndex = 0; noteIndex < notesLength; noteIndex++) {
alert(JSON.infos.info[infoIndex].note.notes[noteIndex].title);
}
}
Putting your json into an var called obj, use the following:
obj.infos.info[0].note.notes[0].title
http://jsfiddle.net/Znq34/
Well the "path" to the JSON notes array-like object is:
json.infos.info[0].note.notes;
So you could do something like:
var notes = json.infos.info[0].note.notes;
var titles = [];
for (var i = 0, len = notes.length; i < len; i++)
{
titles.push(notes[i].title);
}
alert('titles is: ' + titles.join(', '));
Fiddle: http://jsfiddle.net/garreh/uDxqD/
Are you using jQuery? ;-)
// Assuming your using "success" in ajax response
success: function(json)
{
var titles = $(json.infos.info[0].note.notes).map(function() {
return this.title;
}).get();
alert(titles.join(', '));
}
First count the length of notes
var len = jsonobject.infos.info.note.notes.length;
Then loops through and get
var title = jsonobject.infos.info.note.notes[i].title;