1

I have an object like this:

users = [{
  "userid": "1",
  "fornonmods": "<div id=\"user1\" data-login=\"\" data-status=\"online\" class=\"item\">",
  "formods": "<div id=\"user1\" data-login=\"\" data-status=\"online\" class=\"item\">"
}, {
  "userid": "19917",
  "fornonmods": "<div id=\"user19917\" data-login=\"kBr4pelyDy4yKVmiAAAD\" data-status=\"online\" class=\"item\">",
  "formods": "<div id=\"user19917\" data-login=\"kBr4pelyDy4yKVmiAAAD\" data-status=\"online\" class=\"item\">"
}];

I want to replace all the occurrences of
data-login=\"kBr4pelyDy4yKVmiAAAD\" data-status=\"online\"
with
data-login=\"kBr4pelyDy4yKVmiAAAD\" data-status=\"gagged\"
just like I would do with replace() but the thing is that that only works with strings and I am not allowed to convert this object into a string.
What can I do in this cases?

9
  • 1
    so loop over the array and replace the strings. users.forEach Commented Oct 25, 2021 at 17:58
  • care to elaborate a little more please? Commented Oct 25, 2021 at 17:59
  • @CainNuke you don't want to "replace anywhere in object", but in each .fornonmods property of the objects in the array. You know how to iterate an array? Commented Oct 25, 2021 at 18:02
  • not only in fornonmods but also in formods and all others i didnt include for the sake of convinience. Commented Oct 25, 2021 at 18:04
  • @CainNuke But surely not in userids? So list the properties explicitly that you want to process. Commented Oct 25, 2021 at 18:05

2 Answers 2

1

map is what you're looking for

const users = [{
  "userid": "1",
  "fornonmods": "<div id=\"user1\" data-login=\"\" data-status=\"online\" class=\"item\">",
  "formods": "<div id=\"user1\" data-login=\"\" data-status=\"online\" class=\"item\">"
}, {
  "userid": "19917",
  "fornonmods": "<div id=\"user19917\" data-login=\"kBr4pelyDy4yKVmiAAAD\" data-status=\"online\" class=\"item\">",
  "formods": "<div id=\"user19917\" data-login=\"kBr4pelyDy4yKVmiAAAD\" data-status=\"online\" class=\"item\">"
}];

const id = "kBr4pelyDy4yKVmiAAAD";

const res = users.map(x => {
   const container = {};

   container.userid = x.userid,
   container.fornonmods = x.fornonmods.replace(`data-login=\"${id}\" data-status=\"online\"`, `data-login=\"${id}\" data-status=\"gagged\"`);
   container.formods = x.formods.replace(`data-login=\"${id}\" data-status=\"online\"`, `data-login=\"${id}\" data-status=\"gagged\"`);

   return container;
});

console.log(res);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

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

5 Comments

Can I also use a variable for kBr4pelyDy4yKVmiAAAD?
Yes, just use template literals my friend. string text ${yourVar} string text developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
I changed the answer for your request with a const id (kBr4pelyDy4yKVmiAAAD)
it works, thank you!
You're most welcome my friend
1

You need to iterate over users and replace with regex pattern.

const searchString = new RegExp('data-login=\"kBr4pelyDy4yKVmiAAAD\" data-status=\"online\"', 'g');
const replacement = 'data-login=\"kBr4pelyDy4yKVmiAAAD\" data-status=\"gagged\"';
const parsedUsers = users.map(x=>{
  return {
    userid: x.userid,
    fornonmods: x.fornonmods.replace(searchString,replacement),
    formods: x.formods.replace(searchString,replacement)
 }
})

3 Comments

great, but can I use also a variable instead of kBr4pelyDy4yKVmiAAAD?
Of course, just use string concatenation. const searchString = new RegExp(data-.... ${variable}... , 'g')°
Thank you, that was very helpful.

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.