1

I have tried to fetch data using json, and it worked as I wanted to. But the problem is how can I display it on my page or HTML? what I am getting is [{"name":"Black wall"},{"name":"Green wall"},{"name":"Gray wall"}] so How can I make it formal and just display Black wall, Green wall, Gray wall. Is there a way to do this?

$(function(){
    $(".checkBoxClass").on("click", function() {
        var data = [];
        $("table > tbody > tr").each(function () {
        var $tr = $(this);
        if ($tr.find(".checkBoxClass").is(":checked")) {
            data.push({
            name: $tr.find(".name").text(),
            });
        }
        });
        console.clear();
        var messages = JSON.stringify(data);
        console.log(messages);
    });
});
3
  • 1
    Array map() FTW Commented Oct 5, 2021 at 14:59
  • @epascarello how can I do it sir, can you give me a guide? thank you I am new to jquery as you can see thanks Commented Oct 5, 2021 at 15:04
  • Has nothing to do with jQuery it is just basic JavaScript map() method. Commented Oct 5, 2021 at 15:05

2 Answers 2

1

Basic array map() with join()

var data = [{"name":"Black wall"},{"name":"Green wall"},{"name":"Gray wall"}];

console.log(data.map(function(obj){ return obj.name; }).join(", "));

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

4 Comments

this is what I am looking for! thank you very much, I am learning now.
one last question, how can I call it to my HTML? I have <span id="name"></span>
you set the text()
thank yuo very much sir
1

One of the ways that you can convert results to your format:

const myResult = [{"name":"Black wall"},{"name":"Green wall"},{"name":"Gray wall"}];

const myItems= myResult.map( (item)=> {
    return item.name;
});

console.log(myItems.join(','))

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.