Loop through the JSON data and display any empty strings

I encountered an issue while trying to convert an object string from a list of arrays using the JSON.parse method. Despite successfully converting the object string to an object, it appears empty when used within ng-repeat.

Jade

.item.col-md-4(ng-repeat='p in searchData | filter: paginate | orderBy: sortKey ')
   // `p` represents an object of strings, such as "{id:2, name:'abel'}"
   - var property = JSON.parse(""{{ p }}""); // Error occurs at this line
   +AddPropertyCard(property)

SearchData

[{id:0, name:"abel"},{id:1, name:"julia"}]

Error

var property = JSON.parse("");

Unexpected token { in JSON at position 1

Answer №1

Update 1 :

The variable 'p' appears to be an object.

You need to access it using dot notation. For example, {{p.id}} or {{p.name}}. This will display the respective values.


JSON.parse requires a stringified object in order to parse it.

// Let's create a simple object in JavaScript
var notStringObj = {
    "name": "John",
    "age": 30,
    "city": "New York"
};
console.log("JavaScript Object", notStringObj);

// Let's stringify (convert to a string) the object
var stringObj = JSON.stringify(notStringObj);
console.log("JavaScript Stringified Object", stringObj);

// The following code is used to decode this object

// This will give you the expected result - a JavaScript object
var obj1 = JSON.parse(stringObj);

// This will throw an error - Unexpected token { in JSON at position 1
// Because this was a plain object and not a string
// JSON.parse requires a stringified object in order to parse it
var obj2 = JSON.parse(notStringObj)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to stylize a date using Bootstrap-datepicker?

While this topic is well-known, I have a slightly more complex question regarding it. I have gone through the documentation here. My goal is to display the date in French format (dd/mm/yyyy) and save the value in US format (yyyy-mm-dd). Additionally, I nee ...

transform json array into a consolidated array by merging identical IDs

I need to transform an array into a different format based on the values of the ID and class properties. Here is the initial array: const json = [{ "ID": 10, "Sum": 860, "class": "K", }, { "ID": 10, "Sum": 760, "class": "one", }, { "ID": ...

Can a single controller function in ASP.NET MVC4 be used to send back two arrays to the view?

Continuing from my previous query here, I am working with a model called SchoolTestsPerModulePerStudent which looks like this: public partial class SchoolTestsPerModulePerStudent { public long StudentID { get; set; } public long ModuleID { get; se ...

What causes the "Cannot read properties of undefined" error in Vue when an array is initialized and output is still being displayed?

I am working with two vue files, namely App.vue and a component called UsersPage.vue. UsersPage.vue: <script> export default { data:() =>({ usersList : [] }), methods:{ async createUsersList(){ this.usersLi ...

Steps for displaying the JSON data from Google Distance Matrix Results in a printed format

How can I display the results from Google Distance matrix? Here is the format of the results: "rows" : [ { "elements" : [ { "distance" : { "text" : "4,8 km", "value" : 4820 }, "du ...

Utilizing React Native to seamlessly integrate one JavaScript function into another

I am trying to integrate one javascript module into another. I recently downloaded a demo of GiftedMessanger After downloading the GiftedMessanger demo code, I incorporated all its dependencies into my own React Native (ios) project and successfully insta ...

How to extract a JavaScript object from an array using a specific field

When dealing with an array of objects, my goal is to choose the object that has the highest value in one of its fields. I understand how to select the value itself: Math.max.apply(Math, list.map(function (o) { return o.DisplayAQI; })) ... but I am unsur ...

Retrieve the Country Code and Display a Variety of Greetings

I am new to coding and I am working on creating a customized welcome message for my blog that displays different greetings based on the visitor's country. Currently, I have implemented the following code and it is working perfectly. <script src="h ...

What is the most effective method to package a React application?

I am currently working on embedding a React application within a Web component's Shadow Root or an iFrame in order to create a widget for a Chatbot messenger similar to Intercom widget. The technologies I am using include: React 16.8.6 Typescript 3. ...

showing the upload preview and disabling automatic uploading in Dropzone with React

Currently, when the user clicks the upload button and selects a file, it automatically uploads the file. However, I do not want this automatic upload to happen. Instead, I want to display the selected files to the user first before uploading them. I need ...

What is the reason for the failure of this JavaScript promise?

Currently studying the concept of promises. app.get('/message',function(req, res){ var promise = new Promise(function(resolve, reject){ resolve("hi"); }); promise.then(function(message){ res.json(message); }) ...

Application utilizing electron technology featuring various tabbed browsing windows connecting to secure websites

Currently, I am in the process of developing my own unique version of a platform similar to using Electron technology. The objective for this app is to provide users with the ability to view multiple URLs simultaneously, such as and , through a tabbed i ...

Struggling to get Django and AngularJS to play nice?

After opening the file angular.html with AngularJS in Chrome, the table displays the array of information as expected. However, when attempting to use Django with the same angular.html file, the array is not showing up in the table. Unsure of how to procee ...

The comparison between AJAX and JSON passing and PHP generating HTML versus returning it

Currently, my code looks like this: <li onclick = " function CBAppData( callerObj, data ) { var string = ''; for( a in data ) { debug.push( data[ ...

Troubleshooting Protractor in Intellij: A step-by-step guide

While using OSX, I successfully configured an Intellij 15 run/debug task as explained in this post below: How to debug angular protractor tests in WebStorm Although the task runs fine, unfortunately, the debug feature does not work and throws the followi ...

How to include a new variable within a JSON string using C#

I have been working on some code to make it more dynamic. Originally, I used a hardcoded ID in my json string to post a new object with NUnit Test Project using RestSharp. Now, I am attempting to remove the hardcoded object ID and replace it with an empt ...

'this' in Arrow functions does not have a reference to the calling context

Something seems off about the context in this code. I've left comments to describe my issue below: const cat = { //arrow function meow: () => { console.log(this); }, makeMeow(){ // Why does 'this' refer ...

Encountering issues with AngularJS ng-click and $http post functionality

ReactJS theApp.controller('MenuSideController', ['$scope', 'CategoryService', function ($scope, CategoryService){ CategoryService.getList() .success(function(list){ $scope.list = list; }); $scope.me ...

Adding a child node before an empty node in Chrome with JavaScript Rangy

When attempting to insert a node before an empty element at the start of another element, a problem was noticed. Here is the initial setup: <p>|<span />Some text</p> By using range.insertNode() on a Rangy range to add some text, Chrome ...

Hidden IFrame for Jquery File Upload

I was looking for a quick guide on setting up an AJAX-style file upload using a hidden iframe. Below is the section of HTML code related to the form: <div id = "file" class = "info"> <form id="file_upload_form" method="post" enctype=" ...