1

How to convert this in Javascript from:

[
   {
      "label": "Purok I",
      "y": "1"
   },
   {
      "label": "Purok II",
      "y": "1"
   },
   {
      "label": "Purok III",
      "y": "2"
   }
]

to:

[
   {
      label: "Purok I",
      y: 1
   },
   {
      label: "Purok II",
      y: 1
   },
   {
      label: "Purok III",
      y: 2
   }
]

Any help?

8
  • What's the difference? The stringified property names don't do anything. Commented Mar 2, 2019 at 3:07
  • I want to output values with different datatypes but it returns all string Commented Mar 2, 2019 at 3:10
  • Oh, I see - thanks for the explanation. Commented Mar 2, 2019 at 3:11
  • No problem @JackBashford Commented Mar 2, 2019 at 3:12
  • @john what is your intent for converting {"y":"2"} ==> {y:2}? Commented Mar 2, 2019 at 3:15

3 Answers 3

3

This method will update all numeric types in your objects automatically.

let arr = [{
    "label": "Purok I",
    "y": "1"
  },
  {
    "label": "Purok II",
    "y": "1"
  },
  {
    "label": "Purok III",
    "y": "2",
    "example": "432.23"
  }
];

// Map over your array of objects
arr = arr.map(obj => {
  // Map over all the keys in your object
  Object.keys(obj).map(key => {
    // Check if the key is numeric
    if (!isNaN(obj[key])) {
      obj[key] = +obj[key];
    }
  })
  return obj;
});
console.log(arr);

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

2 Comments

What is the difference? All are string
You want it casted?
3

let  p = [
   {
      "label": "Purok I",
      "y": "1"
   },
   {
      "label": "Purok II",
      "y": "1"
   },
   {
      "label": "Purok III",
      "y": "2"
   }
]

let result = p.map(function(x) { 
    x.y = Number(x.y);  
    return x;
});

console.log(result);

1 Comment

You solved my problem . Thank you very much . So much appreciation
3

Make all stringified numbers into non-stringified numbers using map and destructuring like so:

const data = [
   {
      "label": "Purok I",
      "y": "1"
   },
   {
      "label": "Purok II",
      "y": "1"
   },
   {
      "label": "Purok III",
      "y": "2"
   }
];

const numbered = data.map(({ label, y }) => { return {label, y: parseInt(y)}});

console.log(numbered);
.as-console-wrapper { max-height: 100% !important; top: auto; }

EDIT

Turns out making string-less property names is impossible:

var obj = {
  foo: "bar",
  one: 1
};

console.log(obj);

7 Comments

solid answer, doesnt address the fact that he wants to {y: 1} not {"y":1}.
great answer but that's not i wanted
All key and value are string . I want to have different data type, can u help me?
@JohnCrisMañabo It is impossible to do - see the above example.
No problem @JohnCrisMañabo always glad to help.
|

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.