0

I have an array that looks like this

array_names = [Mathew,Kelp ,Liman Kolf, Hebert,Ansh];

Now in the array above, Mathew, Kelp is one name , Liman Kolf is another name and Herbet,Ansh is another name making it 3 names in the array.

Now i want to split this array on new lines in table like below

Mathew,Kelp
Liman Kolf
Herbet,Ansh

But with my code as shown below, the table is represented like this

Mathew 
Kelp 
Liman Kolf
Herbet
Ansh

JS

//how i save to localstorage
 $("#myTableID").on("click", "#add-contact", function() { 
        var values = [];
        value = jQuery(this).closest('tr').find('#user-id').text();
        values.push(value);
        localStorage.setItem('contact_list', values);


}

var array_names = localStorage.getItem('contact_list').split(',');
if(array_names.length)
                {
                    $.each(array_names, function(index, value){
                        $('#myTableID2').append('<tr><td id="contact">'+value+'</td></tr>');
                 });
                }

Controller

$contacts = Contact::where('firstname','LIKE','%'.$q.'%')->orWhere('lastname','LIKE','%'.$q.'%')->get();
13
  • if Mathew and kelp is one name then why there is , in between both of the items Commented Feb 10, 2020 at 12:16
  • That is how the names are stored in the database @JuhilSomaiya Commented Feb 10, 2020 at 12:17
  • 1
    When extracting from the database, you should get a list of strings: ["Mathew,Kelp", "Liman Kolf", "Hebert,Ansh"]. Otherwise, there's no way to tell if the , is the separator or part of the name. Commented Feb 10, 2020 at 12:21
  • 1
    @RoboPHP Please show your full code with how you are saving in localstorage Commented Feb 10, 2020 at 12:30
  • 1
    You might find it easier if you use JSON.stringify when storing the values array and JSON.parse when retrieving it. Or, at the very least, do a values.join(';') so that you can split it later on semicolons. Commented Feb 10, 2020 at 12:42

2 Answers 2

2

You should split them by a RegExp

const string = 'Mathew,Kelp ,Liman Kolf, Hebert,Ansh';
const array = string.split(/(?: ,)|(?:, )/)
// ["Mathew,Kelp", "Liman Kolf, Hebert,Ansh"]
Sign up to request clarification or add additional context in comments.

1 Comment

While this would work for this specific case, relying on the space around , to split the string is dodgy. What if a name suddently has a space?
1

As suggested by @Khauri, use JSON.stringify() and JSON.parse() to read/write from/to localStorage:

// save values to localStorage
localStorage.setItem('contact_list', JSON.stringify(values));

// retrive values from localStorage
var array_names = JSON.stringify(localStorage.getItem('contact_list'));

// do things with the values
if (array_names.length) {
    $.each(array_names, function (index, value) {
        $('#myTableID2').append('<tr><td id="contact">' + value + '</td></tr>');
    });
}

Comments

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.