0

I am loading data and then putting it in an input, the data that it enters goes under the name "Business Type" and I need to change that to "businessTypeId"

for example

actually I have

[{"URL":"http://www.restaurant.com","Business Type":1},{"URL":"http://www.hotel.com","Business Type":2}]

I would like to:

  1. replace all "Business Type" to "businessTypeId"

  2. [delete square brackets]

  3. add "" to id (id is the number in Business Type)

this is the espected result

{"url":"http://www.restaurant.com","businessTypeId":"1"},{"url":"http://www.hotel.com","businessTypeId":"2"}

this is my code

placesArray.push(JSON.stringify(results.data));
console.log(placesArray);
document.getElementById('places').value = placesArray;

placesArray contains the string that I would like to change

2
  • What do you expect from placesArray.push(JSON.stringify(results.data));, and what does console.log(placesArray); log afterwards? As it is an array of string(s), it simply can not be what is shown as "actually I have". Commented Feb 10, 2021 at 23:26
  • that console log is for testing only, ignore it Commented Feb 10, 2021 at 23:28

2 Answers 2

2

You could use the array map function:

const data = [
  { "URL":"http://www.restaurant.com", "Business Type": 1},
  { "URL":"http://www.hotel.com", "Business Type": 2 },
];

const result = data.map(value => ({
  url: value.URL,
  businessTypeId: value["Business Type"]?.toString(),
}));

console.log(result);

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

3 Comments

I get Uncaught TypeError: Cannot read property 'toString' of undefined
That happens if the Business Type field is missing in one of the array values. I'll update my answer to account for the missing field.
thanks, that works, but when I tried to change the string to my placesArray I get an [object Object] result, and if I use JSON.stringify(result) I get only this [{}]
1

const data=
[{"URL":"http://www.restaurant.com","Business Type":1},{"URL":"http://www.hotel.com","Business Type":2}]

//"Buisness Type" is a key, so firstly, go through each index, then in each index search through its keys and do replacing stuffs
data.forEach(a=>{
  Object.keys(a).forEach(b=>{
    if(b=="Business Type"){//what you want replaced
      var n=a[b].toString() //value to put in replaced key(and to string because you told me that you want string data not number data)
      delete(a[b]) //deleting old key
      a["businessTypeId"]=n //adding replaced key with previous value
    }
  })
})

console.log(data)

4 Comments

thanks! but I get a result with [square brackets], I have to delete that [], with your code I had to use JSON.stringify(data); because the result was [object][object]. How can I delete that []? and I have to add "" to id number. for example "businessTypeId":"1"
@AndréCuellarAvaroma for the first thing.. trying to put data into an HTML element would do something like that.. secondly, for the turning 1 to "1" consider that done
when I put the result in an input, puts it with the brackets
I solved adding this .toString().replace(/^[([\s\S]*)]$/,'$1')

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.