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

John S.Web DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.