So I am getting a list of strings from a server,
let a="18647AF0D0,59,6]1864726D1,65,5]1864726A,85,5]1864726A,75,5]";
and with every set of data I am adding a close bracket, which means the set of data is now complete.
So The data look like this
mac = "18647AF0D0";
TTL = "59"
TIME = "6"
And the server continues to send the data, which is added to the variable a
Now This data I need to display on the web server on a HTML site, which has already been designed.
So The issue is here when the server sends sometime it sends a same mac value with a updated TTL and TIME
1864726A,85,5]1864726A,75,5]
Here, if you see, I am getting the same mac value, so what I want to do is if the mac value already exists, then just update the TTL and TIME values on the same position; it should not be creating another entry.
So I was trying to store this value into an array using a split function, but it's getting too complicated, so I am stuck here.
Here is my html page :- https://codepen.io/kanxababu/pen/KKeZKQZ
CodePudding user response:
I didn't understand your question quietly but if you want separate your array with "," and "]" here is the way :
const result = a.split("]").join("").split(",")
CodePudding user response:
This should work for you, just add it in to your existing code. It'll return an array of the position of the array and the position of the element within that array. I think there's probably a syntactically nicer way to do the if statement but I'm not sure, and it would work the same anyways.
function twoDIndexOf (myArray, element) {
let index
for (let i=0;i<myArray.length-1;i ){
if(myArray[i][0].indexOf(element) != -1) {
return [i, myArray[i][0].indexOf(element)]
}
}
}
console.log(twoDIndexOf(array, '1864726A'));