1

I´m trying to update the object atribute:

   data : [
     {"id":1,"rp":"2426","cr":"11-11"},
     {"id":1,"rp":"1119","cr":"19-21"},
     {"id":1,"rp":"3453","cr":"23-81"}
    ]

new object would be updated as below.

NewData: [
 {"id":1,"rp":"2426","cr":"11/11"},
 {"id":1,"rp":"1119","cr":"19/21"},
 {"id":1,"rp":"3453","cr":"23/81"}
]

I looking to update the object cr atribute for all values, for example using javascript.replace() method, I would do replace("-","/").

0

7 Answers 7

2

That's just a simple usage of Array#map and String#replace:

ES2018

const data = [{"id":1,"rp":"2426","cr":"11-11"},{"id":1,"rp":"1119","cr":"19-21"},{"id":1,"rp":"3453","cr":"23-81"}]

const r = data.map(({ cr, ...rest }) => ({ cr: cr.replace('-', '/'), ...rest }));

console.log(r);

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

3 Comments

Maybe worth noting that rest/spread properties are part of ES2018, not ES2015.
Now it looks like you claim your solution can be used with ES2015 and ES2016. But as I said, object rest/spread is ES2018.
@FelixKling As you wish
1

You could use map method to create new array and replace to update cr property.

var data = [{"id":1,"rp":"2426","cr":"11-11"},{"id":1,"rp":"1119","cr":"19-21"},{"id":1,"rp":"3453","cr":"23-81"}]
var update = data.map(({cr, ...rest}) => ({...rest, cr: cr.replace("-","/")}))
console.log(update)

Comments

1

You could iterate and map the array.

var data = [{ id: 1, rp: "2426", cr: "11-11" }, { id: 1, rp: "1119", cr: "19-21" }, { id: 1, rp: "3453", cr: "23-81" }],
    newArray = data.map(o => Object.assign({}, o, { cr: o.cr.replace("-","/") }));

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

Comments

0

You can try this

let data = [
     {"id":1,"rp":"2426","cr":"11-11"},
     {"id":1,"rp":"1119","cr":"19-21"},
     {"id":1,"rp":"3453","cr":"23-81"}
    ];
    
    
let newData = data.map(e=>{
  e.cr = e.cr.replace(/-/, '/')
  return e
})

console.log(newData)

Comments

0

Just try below

data.forEach(function(object){
    object["cr"] = object["cr"].replace("-","/");
});

Comments

0

Try this:

const data = [
  {"id":1,"rp":"2426","cr":"11-11"},
  {"id":1,"rp":"1119","cr":"19-21"},
  {"id":1,"rp":"3453","cr":"23-81"}
];

const newData = data.map(item => ({...item, cr: item.cr.replace(/-/g, '/')}));

console.log(newData);

If you need it to work in IE11:

const data = [
  {"id":1,"rp":"2426","cr":"11-11"},
  {"id":1,"rp":"1119","cr":"19-21"},
  {"id":1,"rp":"3453","cr":"23-81"}
];

const newData = data.map(
  function(item) {
    return {
      id: item.id,
      rp: item.rp,
      cr: item.cr.replace(/-/g, '/')
    }
  }
);

console.log(newData);

2 Comments

This looks as if it would be pure, but it actually isnt.
Updated to not affect original obj
0

const data = [{"id":1,"rp":"2426","cr":"11-11"},{"id":1,"rp":"1119","cr":"19-21"},{"id":1,"rp":"3453","cr":"23-81"}]

const r = JSON.parse(JSON.stringify(data).replace(/-/g,'/'))
console.log(r);

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.