• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 35
  • Last Modified:

Array Help - searching for a value

I simply want my function to return the following HTML:

return "<span>PHONE NAME HERE</span>";

Open in new window


I will be passing the function the ID. Can anyone help me fill in the blanks? Do I have to use a bunch of IF's? There are alot of models in my lookup array, so I'd rather not.

function getModel(id){

  return "<span>" + YOUR BRILLIANT CODE HERE + "</span>";

}

Open in new window


Array ( my "lookup" array looks like this: )


[
  {
    "id": 1,
    "name": "iPhone 5",
    "oemId": 1,
    "rank": null,
    "oem": null,
    "active": true
  },
  {
    "id": 2,
    "name": "iPhone 6",
    "oemId": 1,
    "rank": null,
    "oem": null,
    "active": true
  }
]

Open in new window

0
John S.
Asked:
John S.
2 Solutions
 
Eranga JayalatharachchiTech LeadCommented:
function getModel(id, lookUpArray){
    var i = lookUpArray.map(function(e){ return e.id; }).indexOf(id);
    return "<span>" + ((-1 === i) ? '' :  lookUpArray[i].name) + "</span>";
}

Open in new window

0
 
Fernando Bravo DiazCommented:
Here is a little example of how you can get the name:
<!DOCTYPE html>
<html>
<head>
	<title></title>
</head>
<body onload="showModels()" id="myBody">
	<script type="text/javascript">
		var json = [
		  {
		    "id": 1,
		    "name": "iPhone 5",
		    "oemId": 1,
		    "rank": null,
		    "oem": null,
		    "active": true
		  },
		  {
		    "id": 2,
		    "name": "iPhone 6",
		    "oemId": 1,
		    "rank": null,
		    "oem": null,
		    "active": true
		  }
		];

		function getModel(id){
                        //read all array elements
			for (var i = 0; i < json.length; i++) {
				if(json[i].id == id){
                                        //return the element name
					return "<span>"+json[i].name+"</span>"
				}
			}
		}

		function showModels(){
			document.getElementById("myBody").innerHTML = getModel(1) + "<br>" +getModel(2);
		}	

	</script>

</body>
</html>

Open in new window

0
 
Julian HansenCommented:
I don't think you want to use map here
var model = [
  {
    "id": 1,
    "name": "iPhone 5",
    "oemId": 1,
    "rank": null,
    "oem": null,
    "active": true
  },
  {
    "id": 2,
    "name": "iPhone 6",
    "oemId": 1,
    "rank": null,
    "oem": null,
    "active": true
  }
];
function getModel(id) 
{
  var name='';
  // IF IE IS NOT A REQUIREMENT YOU CAN USE find() HERE
  for(var i = 0; i < model.length; i++) {
    if (model[i].id == id) {
      name = model[i].name;
      break;
    }
  }

  return "<span>" + name + "</span>";
}
var result = getModel(2);

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Julian HansenCommented:
@Fernando Bravo Diaz
What if the requested id does not exist - your function has an exit point that does not return a value.
1
 
John S.Web DeveloperAuthor Commented:
I don't understand why this wouldn't let me split the points?!?!?!? Did they change the way this works? It did not give me the option like it used to.
0
 
Fernando Bravo DiazCommented:
I've made a little change:
function getModel(id){
			for (var i = 0; i < json.length; i++) {
				if(json[i].id == id){
					return "<span>"+json[i].name+"</span>"
				}
			}
                   //if the id doesn't exist
			return "";
		}

Open in new window

that way if i enter an id that isn't an id contained in the json, it will return and empty string.
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now