Getting only one set of values


#1

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) {
		 
		 $.meow({

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

#2

Don’t use a loop …?


#3

How do you do it?


#4

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


#5

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


#6

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',

#7

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.


#8

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?


#9

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