Creating And Assigning Values To An Object

I have a string consisting of text and HTML tags. I would like to create a new javascript object every time the < character is detected and assign a value to the leftAngleBracketPosition property of the newly created object and every time the > character is encountered assign the rightAngleBracjetPosition property and push this object into an array. I tried to do this with a for loop but it doesn’t work. Below is my pseudo code

tagsArray = empty array
mystring = a string containing text and HTML tags
for i = 0, i < mystring.length
if mystring.charAt(i) == ‘<’
create a new object called tag
set it’s leftAngleBracketPosition property to i
else if mystring.charAt(i) == ‘>’
set it’s rightAngleBracket property to i
tagsArray.push(tag)

Hi @liagapi555, that pseudo code should kinda work… you’ll have to post the actual code so we can can help you debugging. Parsing HTML “manually” is notoriously difficult though – e.g. you’ll have to take care of nested tags and characters inside strings. Is there a specific reason why you can’t use the regular DOM API here? E.g. using the DOM parser:

const parseHTML = string => new DOMParser()
  .parseFromString(string, 'text/html')
  .body
  .querySelectorAll('*')

console.log(
  parseHTML('<div><h1>hello</h1><p>lorem ipsum</p></div>')
) // [div, h1, p]

Or just in general - why you want to do this. It seems an odd sort of thing to do, so perhaps if we knew your use case we might be able to suggest a better course of action.

Hi m3g4p0p, I guess the real question I have is after I created a new object and assigned this object to a global variable in a for loop, assigned values to its properties and stored this object in an array if a certain condition is met, do the values of the properties of this object stored in the database change when the loop iterates which creates a new object, then new values are given to its properties and then it gets assigned to the same global variable. In other words is deep copy or shallow copy performed in this situation. Please see my code snippet below for reference.

var tags = [];
var tag;
var inputString = "<div>Test</div>"
var leftAngleBracketFound = false;
for(let i = 0; i<inputString.length; i++){
   if(inputString.charAt(i) == '<'){
      leftAngleBracketFound = true;
      tag = new Object();
      tag.leftAngleBracketPos = i;      
   }else if( leftAngleBracketFound && (inputString.charAt(i) == '>'){
      tag.rightAngleBracketPos = i;      
      leftAngleBracketFound = false;
      tags.push(tag);
  }
}

If you push an object to an array, then it is not getting copied at all but pushed by reference. However after you reassigned a new object to the same variable, then the reference to the previous object is only held in the array:

var array = []
var object

object = {}
object.value = 'foo'
array.push(object)

object = {}
object.value = 'bar'
array.push(object)

console.log(array) // [{ value: 'foo' }, { value: 'bar' }]
object.value = 'baz'
console.log(array) // [{ value: 'foo' }, { value: 'baz' }]

PS: Which database do you mean though?

I meant to say say array not database. My mind was on so many things when I wrote the question that I didn’t realize I had written database instead of array.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.