-1

I have an object:

let Data = {name: 'Flomo', age: 25, address: 'Sinkor', id: 'NMF25'}

I want to console log the entire object but the id value should only contain the last three characters.

Like this:

{name: 'Flomo', age: 25, address: 'Sinkor', id: 'F25'}

I'm doing this:

console.log(Data.replace(/NM/g, ''))

But I got:

TypeError: Data.replace is not a function

Is there an easy way to achieve this?

Israel Z Kollie
  • 273
  • 5
  • 17
  • 2
    What is `Data.replace`? How would JS know you're going to replace characters in the `id` property if you don't refer to it anywhere in your code? – zerkms Jun 29 '17 at 00:00
  • How would it know what property you want to manipulate? Just call `replace` on whichever property... – Brad Jun 29 '17 at 00:02
  • You could use `Data.id.substring(Data.id.length-3);` That way if the `id` is more or less characters it will still only return the last 3 and if the ID isn't force to start with `NM` this wil still work. To test the output add `console.log(Data.id.substring(Data.id.length-3));` – NewToJS Jun 29 '17 at 00:04

2 Answers2

4

replace is a method that operates on String, and doesn't change the string in-place.

If you're not worried about changing the original data, you can do this:

let Data = {name: 'Flomo', age: 25, address: 'Sinkor', id: 'NMF25'}
Data.id = Data.id.replace('NM', '')
console.log(Data);

Alternatively, if you're not sure what the first characters of the id will be (or how many there could be), you could do this:

let Data = {name: 'Flomo', age: 25, address: 'Sinkor', id: 'NMF25'}
Data.id = Data.id.substring(Data.id.length-3)
console.log(Data);

If you need to keep the original data intact, you can copy the object, however this can be complicated depending on the data that might be in the object: How do I correctly clone a JavaScript object?

Clonkex
  • 3,373
  • 7
  • 38
  • 55
  • @zerkms well presumably the OP doesn't want to edit the original data, only to log a version of it that doesn't contain all the characters of the `id` property. – Clonkex Jun 29 '17 at 00:04
  • Your code certainly modifies the original data. Check `console.log(Data);` – zerkms Jun 29 '17 at 00:04
  • I think using `Data.id.substring(Data.id.length-3);` would be much easier if the ID length ever changes and or it doesn't always start with `NM` – NewToJS Jun 29 '17 at 00:06
  • @NewToJS Depends, we don't know what the OP's data looks like. He/she might know that replace will work fine. However I'll add that to my answer as an option. – Clonkex Jun 29 '17 at 00:07
  • @Clonkex Very true but I think it's always best to try cover the what if's when possible as some don't think of changes later in the future or maybe changes out of their hands. – NewToJS Jun 29 '17 at 00:08
  • 1
    Much better, multiple choice on solutions +1 :) – NewToJS Jun 29 '17 at 00:15
0

The replace function works on a String. Youre calling that function on an Object.

You probably intended to call replace on the value of the property id. The terms I just used should help you mentally step thru problems like this in the future!

If you look at MDN's documentation for JavaScript, you'll see which functions you can call on which Types and other objects.

MiiinimalLogic
  • 820
  • 6
  • 11