3

Actually, I am receiving array in response with slashes so, I did this.

what I receiving on the response

[{\"name\":\"title\",\"value\":\"%post_title%\"},{\"name\":\"author\",\"value\":\"%author_name%\"}]

so I did this

var b=JSON.stringify(response.data);
var str = b.replace(/\\/g, '');

after this, I have a string like

["{"name":"title","value":"%post_title%"}","{"name":"author","value":"%author_name%"}","{"name":"wordcount","value":"%wordcount%"}","{"name":"logged_in","value":"%logged_in%"}","{"name":"page_id","value":"%page_id%"}","{"name":"post_date","value":"%post_date%"}"]

now, how can I again covert this to the array so I use it in ng-repeat?

2
  • 2
    You want to try JSON.parse Commented Oct 13, 2018 at 5:41
  • sorry, i don't know about JSON.parse, if I use that can I convert string as an array ? can you give an example here Commented Oct 13, 2018 at 5:43

4 Answers 4

2

You should use JSON.parse() to create an array:

let str = '[{\"name\":\"title\",\"value\":\"%post_title%\"},{\"name\":\"author\",\"value\":\"%author_name%\"}]';

console.log(JSON.parse(str));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

Comments

1

You can use JSON.parse for that.

const myNewArray = JSON.parse(str);

Comments

0

use angular.fromJson()

$scope.x = '[{\"name\":\"title\",\"value\":\"%post_title%\"},{\"name\":\"author\",\"value\":\"%author_name%\"}]';

 console.log(angular.fromJson($scope.x));

otherwise use JSON.parse()

console.log(JSON.parse($scope.x));

Comments

-1

You can use JSON.parse():

var array = JSON.parse(`[
    {"name":"title",  "value":"%post_title%"}, 
    {"name":"author", "value":"%author_name%"}
]`);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.