Getting only one set of values

How can I get just one set of values …like " val1 : ‘text1’" The way it’s working now is it wants to print all the values.

var data = {
    val1 : 'text1',
    val2 : 'text2',
    val3 : 'text3'

jQuery(document).ready(function ($) {
$(document).ready(function() {

$.each(data, function(key, value) {

          message: '' + ProductName + 'About ' + Ago + key + value + 'ago',
          icon: 'image1.jpg',
		  title: Title + " in " + Location + " just ordered"

Don’t use a loop …?

How do you do it?

It depends. Does it matter which key value pair you get?

As long as they match. var1 would always match key1 etc.

Because var1 will be the same as key1, you can easily do away with the each loop and get the value directly by using var1 as the key. eg.

... + var1 + data[var1] + 'ago',

Depending on what exactly you’re trying to achieve, you might use Object.entries() to get an array of the key / value pairs…

var entries = Object.entries(data)

console.log(entries[0]) // ['val1', 'text1']
console.log(entries[2]) // ['val3', 'text3']

Unfortunately there is no IE support though (and probably never will be), but the MDN link above also provides a polyfill.

When you call $.each with an object, jQuery iterates over each of the properties in that object.

There is no guaranteed order in which those properties of the object are listed or stored. Does that matter with what you’re doing?

No it doesn’t matter what order they will be listed. Maybe randomly.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.