0

i'm working with angular js and i have the following array that contains many json object , each objects has the property "isSingle" which contains "true" or "false" , my issue is how to convert this property to boolean true or false :

[
    {
      "noFolder": "AW343",
      "type": "T7",
      "creationDate": "22/05/2017",
      "isSingle": "true"
    },
    {
      "noFolder": "AW35",
      "type": "T34",
      "creationDate": "09/05/2017",
      "isSingle": "false"
    },
    {
      "noFolder": "ASW3",
      "type": "T3",
      "creationDate": "05/07/2017",
      "isSingle": "true"
    },
    {
      "noFolder": "BG5",
      "type": "T1",
      "creationDate": "22/12/2018",
      "isSingle": "false"
    }

]

my desired result is to have objects like :

  {
      "noFolder": "ASW3",
      "type": "T3",
      "creationDate": "05/07/2017",
      "isSingle": true
    }

do you have any idea about how to change the type of the property isSingle to bbolean. i'm using angularjs...

4
  • Please read What is the difference between JSON and Object Literal Notation? Commented May 31, 2018 at 13:27
  • If you are using angular, shouldn't you be using Typescript instead of Javascript ? Commented May 31, 2018 at 13:27
  • Possible duplicate of How can I convert a string to boolean in JavaScript? Commented May 31, 2018 at 13:30
  • Would it not be better to tackle the problem at the server API and have it return valid JSON for boolean values like "{...,"isSingle":true}"? Commented May 31, 2018 at 14:15

3 Answers 3

4

Simple loop through the objects to convert string to bool:

yourArray.forEach(x => x.isSingle = x.isSingle === 'true');
Sign up to request clarification or add additional context in comments.

Comments

1

Just Iterate over the array and replace the value as per your requirement like this -

var obj = [
    {
      "noFolder": "AW343",
      "type": "T7",
      "creationDate": "22/05/2017",
      "isSingle": "true"
    },
    {
      "noFolder": "AW35",
      "type": "T34",
      "creationDate": "09/05/2017",
      "isSingle": "false"
    },
    {
      "noFolder": "ASW3",
      "type": "T3",
      "creationDate": "05/07/2017",
      "isSingle": "true"
    },
    {
      "noFolder": "BG5",
      "type": "T1",
      "creationDate": "22/12/2018",
      "isSingle": "false"
    }

]

obj.map((e) => {
    e.isSingle == "true" ? e.isSingle = true : e.isSingle = false
});

2 Comments

@str I tried it on my console, is there any error in this code ?
Yes, Agree with your point. and yes it modifies original object
0

Using reduce method.

let updated_array = array.reduce(function(acc, elem) {
    acc.push(Object.assign({}, elem, { isSingle: (elem['isSingle'] == true ? true : false) }));
    return acc;
}, []);

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.