0

I have a set of arrays in json as followed:

{"status":200,"success":true,"result":
[{"id":"0","NameInData":"","name":"","type":"mf","greeting":"Bonjour","message":"GENERIC: Lorem ipsum."},
{"id":"1","NameInData":"alexis","name":"Alexis","type":"mf","greeting":"Bonjour Alexis","message":"ONE: Lorem ipsum."},
{"id":"2","NameInData":"laurence","name":"Laurence","type":"ff","greeting":"Hello Laurence","message":"TWO: Lorem ipsum."},
{"id":"3","NameInData":"francois","name":"Francois","type":"mm","greeting":"Konnichiwa Francois","message":"THREE: Lorem ipsum."},
{"id":"4","NameInData":"dirk","name":"Dirk","type":"mf","greeting":"Ni hao Dirk","message":"FOUR: Lorem ipsum."},
{"id":"5","NameInData":"coline","name":"Coline","type":"ff","greeting":"high 5! Coline","message":"FIVE: Lorem ipsum."}]}

I am able to identify the value of a key from this array, and //do something with it with the following code:

$.each(json.result, function(){
    if (this.name == content){
        //do something
        var identifiedID =  this.id;
        console.log("PRINT ID: " + identifiedID);

        var name = json.result[identifiedID].name;                  
        var $nameText = $("#nameText"),
            str = name;
            html = $.parseHTML(str),
            nodeNames = [];
            $nameText.append(html);
        console.log("Name: " + name);
    } else {
        var identifiedID = 0;
        console.log("PRINT ID: " + identifiedID);   

        var name = content;                 
        var $nameText = $("#nameText"),
            str = name;
            html = $.parseHTML(str),
            nodeNames = [];
            $nameText.append(html);
        console.log("Name: " + name);
});

I am trying to //do other things if the name is not unidentified within the list with the following code. However, as my array has 6 items, the output would be repeating the whole process 6 times, where I would only want one output, one result (either, a name and its id has been identified and print what it is related to OR a name has not been recognised and the id would be set to 0 and print what it is related.)

You may find my full code in the link here. It would be very nice if someone could tell me which part of my syntax is wrong please?

3
  • You referred to json object in your question but your fiddle is missing that. Commented Oct 30, 2015 at 16:14
  • Uh...can you always be sure that {"id": "3"... will be at index [3]? Because if that's not a certainty, I can see a gap in your logic. Commented Oct 30, 2015 at 16:21
  • Shouldn't this be moved to codereview.stackexchange.com ? Commented Oct 30, 2015 at 16:28

4 Answers 4

1

You can use Array.prototype.find to find the first element that matches a condition (i.e. name == content), defaulting to the first element of json.result if none is found (assuming json.result[0] is always the default case).

var item = json.result.find(function(e) {
    return e.name == content;
}) || json.result[0];

console.log("PRINT ID: " + item.id);

var name = item.name || content;                  
var $nameText = $("#nameText"),
    str = name;
    html = $.parseHTML(str),
    nodeNames = [];
    $nameText.append(html);
console.log("Name: " + name);

Note this code makes the assumption that name != "" for non-default items that you're looking for.

I'm not sure what you're doing with nodeNames but if you're just trying to set the text of #nameText to the name, you can simply use $('#nameText').text(name);

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

Comments

1

you could just exit the each function in your if statement once your condition is met with.

return false;

Comments

1

In the JSFiddle code you should use $.grep() instead of $.each() to filter out items matching your condition, i.e.- name. $.grep() returns the array of matched elements. So you can check if the the length of the array is greater than 0. If yes you have found a match. If not there is no matching element.

Check http://api.jquery.com/jQuery.grep/

Comments

0

Answering the question I would say that syntax looks correct...

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.