could not read json data using jquery with for loop - javascript

I have a data object and inside that I have data object with json data inside. Here is how is looks like. the name of the file is data.js
var data =
[
{
title: 'this is title one',
commit: 'this is commit'
},
{
title: 'this is title two',
commit: 'this is commit'
},
{
title: 'this is title three',
commit: 'this is commit'
},
{
title: 'this is title four',
commit: 'this is commit'
},
{
title: 'this is title five',
commit: 'this is commit'
},
{
title: 'this is title six',
commit: 'this is commit'
},
{
title: 'this is title seven',
commit: 'this is commit'
}
]
here is my code
for (var i = 0; i < data.length; i++) {
$(container).load(view, function () {
console.log(data[i].title);
li.append('<img src="../images/profile.png" alt="Profile photo" />');
li.append('<span class="commit">' + data[i].title + '</span>');
li.append('<p>' + data.commit + '</p>');
li.append('<section class="clear"></section>');
$('#home ul').append(li);
});
}
Now I am getting this error in my console
Cannot read property 'title' of undefined
above error is on this line
li.append('<span class="commit">' + data[i].title + '</span>');
how to fix this?

Well, data.js is not really json.
First switch from load() to getJSON()
$.getJSON(view, function(data) { })
next remove the line
var data =
just have the json contents. jQuery will assign JSON to argument data.

Your json array contains errors.
You can validate your json here http://json.parser.online.fr/
Must be so:
{
"title": ["this is title one", "this is title two", "this is title three", "this is title four", "this is title five", "this is title six", "this is title seven" ]
,
"commit": [ "this is commit", "this is commit", "this is commit", "this is commit", "this is commit", "this is commit", "this is commit" ]
}

Related

javascript error SyntaxError: missing : after property id

Trying to make an array of strings inside array of objects and getting an error saying SyntaxError: missing : after property id for the code below in javascript
let movies = [
{
movie title: 'Avengers: Age of Ultron',
release year: '2015',
rating:'7.3',
"genres"=[0:"Action",1:"Adventure",2:"Sci-Fi"]
},
{
movie title: 'The Dark Knight',
release year:'2008',
rating:'9.0',
genres:["Action","Crime","Drama"]
},
{
movie title: 'Forrest Gump',
release year:'1994',
rating:'8.8',
genres:["Drama","Romance"]
},
{
movie title: 'Inception',
release year:'2010',
rating:'8.8',
genres:["Action","Adventure","Sci-Fi"]
},
{
movie title: 'The Matrix',
release year:'1999',
rating:'8.7',
genres:["Action","Sci-Fi"]
},
{
movie title: 'Border',
release year:'1997',
rating:'7.9',
genres:["Action","Drama","History"]
}
];
There are two mistakes in your code.
If JSON object key contains ' ', you should write ".
You don't need to write an index to your JSON object value.
Here's the updated code.
let movies = [
{
"movie title": 'Avengers: Age of Ultron',
"release year": '2015',
rating:'7.3',
genres:["Action", "Adventure", "Sci-Fi"]
},
{
"movie title": 'The Dark Knight',
"release year":'2008',
rating:'9.0',
genres:["Action","Crime","Drama"]
},
{
"movie title": 'Forrest Gump',
"release year":'1994',
rating:'8.8',
genres:["Drama","Romance"]
},
{
"movie title": 'Inception',
"release year":'2010',
rating:'8.8',
genres:["Action","Adventure","Sci-Fi"]
},
{
"movie title": 'The Matrix',
"release year":'1999',
rating:'8.7',
genres:["Action","Sci-Fi"]
},
{
"movie title": 'Border',
"release year":'1997',
rating:'7.9',
genres:["Action","Drama","History"]
}
];

How can I make a Search String to search like an IN filter on an Array of Object

The filter on UI allows a user to search using the IN criterion. The search string will then become the property of the condition Object.
The search string will look like "1234,5436,8765" and then the condition Object will be like ```
condition: {
field: "Id",
operator: "in",
value: "1234,5436,8765"
}
Now as this is going to be IN filter, so whar criterion should I use so as to make the value search like IN criterion from the results Array.
e.g. for a like filter, I will set my value property like this %1234% so as to search this in the results Array.
Your question isn't clear so try to edit it to make it clearer.
I made a huge assumption here in my answer (as it is not clear from the question):
You could do it with a switch statement and then your various filter methods for each case.
You could do it more robustly, but until the question is clearer, it doesn't make much sense to build this solution out.
const myArrOfObs = [
{Id: 1234, title: "some title 1", other: "some other val 1"},
{Id: 2468, title: "some title 2", other: "some other val 2"},
{Id: 8240, title: "some title 3", other: "some other val 3"},
{Id: 9371, title: "some title 4", other: "some other val 4"},
{Id: 5436, title: "some title 5", other: "some other val 5"},
{Id: 8365, title: "some title 6", other: "some other val 6"},
{Id: 8765, title: "some title 7", other: "some other val 7"},
{Id: 3946, title: "some title 8", other: "some other val 8"}
];
const condition = {
field: "Id",
operator: "in",
value: "1234,5436,8765"
};
function filterArr(arr, cond) {
switch (cond.operator){
case "in":
const valueArr = cond.value.split(',');
return arr.filter(item => valueArr.includes(item[cond.field].toString()));
//case "like":
//return arr.filter( ... );
default:
return null;
}
}
const myFilteredArr = filterArr(myArrOfObs, condition);
console.log(myFilteredArr);

How to get full values in [Array]?

i have a POST request with some Data, i post with Postman , Data like this , But i have some problem is when i console.log(newLesson) i cannot get value in [Array] , but when i try console.log(req.body.quizs) its can !!! Can some one explain to me why ? And how can i get values from that ? Many thanks 😊
"name":"This is name",
"videoId" : "this is videoId",
"level" : "beginner",
"script" : "this is Script",
"quizs" : [
{
"question" : "This is question 1",
"answer" : [
"Answer 1",
"Answer 2",
"Answer 3"
],
"correct_Answer" : "Answer 3"
},
{
"question" : "This is question 2",
"answer" : [
"Answer 1",
"Answer 2",
"Answer 3"
],
"correct_Answer" : "Answer 1"
}
]
}
And this is my backend when received
module.exports.addNewLesson = function(req,res){
let newLesson = {
name : req.body.name,
videoId : req.body.videoId ,
level : req.body.level,
script : req.body.script,
quizs : req.body.quizs
};
console.log(newLesson);
}
And this is what i got
{
name: 'This is name',
videoId: 'this is videoId',
level: 'beginner',
script: 'this is Script',
quizs: [
{
question: 'This is question 1',
answer: [Array],
correct_Answer: 'Answer 3'
},
{
question: 'This is question 2',
answer: [Array],
correct_Answer: 'Answer 1'
}
]
}
How to can i get answer : [Array] ??? Thanks
Try stringifying the output with formatting:
console.log(JSON.stringify(newLesson, null, 4)); // 4 is the number of whitespaces used for formatting

Angular - Get array from JSON file

Sorry for the noob question but I am just getting started with Angular. I have this array declared in services.js:
var articles = [{
id: 1,
title: 'Article 1 Title',
intro: 'Article 1 Intro',
image: 'photo.jpg',
published: '27/10/2016',
text: 'Article 1 Text',
url: 'http://www.domain.com'
}, {
id: 2,
title: 'Article 2 Title',
intro: 'Article 2 Intro',
image: 'photo.jpg',
published: '27/10/2016',
text: 'Article 2 Text',
url: 'http://www.domain.com'
}];
Now that everything works fine in my Angular App I want to read this array from a JSON file that can be found, for example, at www.domain.com/articles.json and it looks like this:
{
"articles": [
{
"id": 1,
"title": "Article 1",
"intro": "Article 1 Intro",
"image": "image.png",
"published": "27/10/2016",
"text": "Article 1 Text",
"url": "http://www.domain.com/article-1"
},
{
"id": 2,
"title": "Article 2",
"intro": "Article 2 Intro",
"image": "image.png",
"published": "27/10/2016",
"text": "Article 2 Text",
"url": "http://www.domain.com/article-2"
}
]
}
How can I make this change?
To get the data of the file in your Angular app, you'll have to get the file's contents through an HTTP request. The easiest way to do this is by using the $http service.
Example (with relative url):
$http.get('/articles.json').then(function (response) {
var articles = response.data.articles;
// Do something with articles...
});
The callback function you're passing to the then method will be called when the HTTP request was successful. Note that your data will only be available in this callback function. Also don't forget to include a dependency to the $http service in your controller. ;)

AngularJS get certain parameters from $resource query request

I have object which I retrieved like this $scope.posts = posts.query()
posts is ngResrouce
angular.module('flapperNews')
.factory('posts', ['$resource', function($resource) {
return $resource('api/posts.json/:id', {id: "#id"})
}]);
Retrieved object
[
0: {
$hasKey: "object:4",
title: "some text",
content: "hello world there",
upvotes: 0
}
1: {
$hasKey: "object:5",
title: "Lorem ipsum is dolor",
content: "hello world there",
upvotes: 0
}
2: {
$hasKey: "object:6",
title: "When there is nothing to share",
content: "hello world there",
upvotes: 0
}
]
How can I get just "titles" from this retrieved object so I can use it for
AngularJS Formly validation? (validating if the new post title would be unique)

Categories

Resources