最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Change value in JSON array and return updated array JS - Stack Overflow

programmeradmin5浏览0评论

I've got a JSON object where I get a product price (number) as a value.

What I want is to convert the price values into strings in the existing object

I use map function:

var prods = [
  {
    "id": "id-1",
    "price": 239000,
    "info": "info-1"
  },

  {
    "id": "id-2",
    "price": 90770,
    "info": "info-2"
  },

  {
    "id": "id-3",
    "price": 200787868,
    "info": "info-3"
  },
];


prods.map(function(a) {
         a.price.toString()
        .replace(/(\d)(?=(\d{3})+(\D|$))/g, '$1 ');
})

I expect the map function to update my prods object, but it stays the same.

(Un)working JS Bin

I wonder, how to update the initial object to the one I need?

I've got a JSON object where I get a product price (number) as a value.

What I want is to convert the price values into strings in the existing object

I use map function:

var prods = [
  {
    "id": "id-1",
    "price": 239000,
    "info": "info-1"
  },

  {
    "id": "id-2",
    "price": 90770,
    "info": "info-2"
  },

  {
    "id": "id-3",
    "price": 200787868,
    "info": "info-3"
  },
];


prods.map(function(a) {
         a.price.toString()
        .replace(/(\d)(?=(\d{3})+(\D|$))/g, '$1 ');
})

I expect the map function to update my prods object, but it stays the same.

(Un)working JS Bin

I wonder, how to update the initial object to the one I need?

Share Improve this question asked Jan 31, 2017 at 13:38 Alexandr BelovAlexandr Belov 1,8349 gold badges31 silver badges44 bronze badges 2
  • 1 map returns a new array, so you have to do something like newarr = prods.map(function(a) { return a.price.toString() .replace(/(\d)(?=(\d{3})+(\D|$))/g, '$1 '); }); – Loksly Commented Jan 31, 2017 at 13:40
  • note the "return" is necessary – Loksly Commented Jan 31, 2017 at 13:40
Add a ment  | 

5 Answers 5

Reset to default 6

Strings are immutable.

String#replace returns a new string and therefore you need an assignment.

And while you only need to change one property, you could use Array#forEach.

var prods = [{ id: "id-1", price: 239000, info: "info-1" }, { id: "id-2", price: 90770, info: "info-2" }, { id: "id-3", price: 200787868, info: "info-3" }];

prods.forEach(function(a) {
    a.price = a.price.toString().replace(/(\d)(?=(\d{3})+(\D|$))/g, '$1 ');
});

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

You need to assign value back to 'price' after replace

prods.map(function(a) {
         a.price = a.price.toString()
        .replace(/(\d)(?=(\d{3})+(\D|$))/g, '$1 ');
})

you need to use this one

Sample Code

prods.map(function(a) {
             a.price = ""+a.price;
})

Note that map apply a function to each element on array, you could return new array, it won't modify existing array, so you need assign new array to some variable.

var prods = [
  {
	"id": "id-1",
	"price": 239000,
	"info": "info-1"
  },
  
  {
	"id": "id-2",
	"price": 90770,
	"info": "info-2"
  },
  
  {
	"id": "id-3",
	"price": 200787868,
	"info": "info-3"
  },
];


prods = prods.map(function(a) {
  a.price = a.price.toString()
   .replace(/(\d)(?=(\d{3})+(\D|$))/g, '$1 ');
  return a;
});
console.log(prods);

You only update the price inside the lambda passed to the map function. You should create a new array where you store the new values.

let newArray= [];
prods.map(function(a) {
     a.price = a.price.toString()
    .replace(/(\d)(?=(\d{3})+(\D|$))/g, '$1 ');
     newArray.push(a);
})

This would do the trick and lets your original array untouched.

Try it out here in the Ramda REPL

Regards, Felix

发布评论

评论列表(0)

  1. 暂无评论