How to use the pairs function from underscore

Find comprehensive JavaScript underscore.pairs code examples handpicked from public code repositorys.

Underscore.pairs is a JavaScript function that converts an object into an array of key-value pairs, where each pair is represented by a two-element array containing the key and its corresponding value.

79
80
81
82
83
84
85
86
87
88
89
//async function savePageMods(pageMods) {
//	pageMods = pageMods || glPageMods;
//	const toBeSavedObj = _.mapObject(pageMods, (val, key) => {
//		return val.toString();
//	});
//	const toBeSavedStr = "module.exports = {" + _.pairs(toBeSavedObj).map(x => ("\"" + x[0] + "\": " + x[1])).join(",\n") + "}";
//	return await fs.promises.writeFile(pageModsF, toBeSavedStr, "utf8");
//}


// END service Funcs
fork icon0
star icon0
watch icon1

33
34
35
36
37
38
39
40
41
42
generateParams() {
    // takes valid pie data and massages it in a pie-compatible format
    // pie data must be an array of [label, value] arrays
    return {
        data: {
            columns: utils.isJSONObject(this.data) ? _.pairs(this.data) : this.data,
            type: 'pie'
        },
        transition: { duration: 0 }
    }
fork icon0
star icon0
watch icon2

+ 5 other calls in file

How does underscore.pairs work?

Underscore.pairs works by taking an object as its argument and returning an array of arrays where each inner array contains two elements: the key of a property in the object and its corresponding value. The function iterates over each property in the object and creates a two-element array for each key-value pair. The first element of each inner array is the key of the property and the second element is the corresponding value. The function then pushes each of these two-element arrays into a new array, which is returned as the result of the _.pairs function. Note that the order of the key-value pairs in the resulting array is not guaranteed to be the same as the order in which they were defined in the original object.

21
22
23
24
25
26
27
28
29
30
render() {
    var filteredData = this.filteredData();
    var dictVal = _.isArray(filteredData) ? filteredData[this.current] : filteredData;

    $(this.el).html(cardDataViewTemplate({
        values: _.pairs(dictVal),
        searchQuery: this.searchQuery,
        currentCard: this.current + 1,
        numCards: filteredData.length
    }));
fork icon0
star icon0
watch icon2

22
23
24
25
26
27
28
29
30
31

async function savePageMods(pageMods) {
	const toBeSavedObj = _.mapObject(pageMods, (val, key) => {
		return val.toString();
	});
	const toBeSavedStr = "module.exports = {" + _.pairs(toBeSavedObj).map(x => ("\"" + x[0] + "\": " + x[1])).join(",\n") + "}";
	return await fs.promises.writeFile(pageModsF, toBeSavedStr, "utf8");
}

function readPageMods(){
fork icon0
star icon0
watch icon0

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
const _ = require("underscore");

const myObj = {
  name: "John",
  age: 30,
  city: "New York",
};

const pairsArray = _.pairs(myObj);

console.log(pairsArray);
// Output: [["name", "John"], ["age", 30], ["city", "New York"]]

In this example, we first import the underscore library and then define an object with some properties. We then use _.pairs to create a new array of key-value pairs, where each inner array contains a key-value pair from the original object. The resulting array is [['name', 'John'], ['age', 30], ['city', 'New York']]. Note that the order of the pairs in the resulting array is not guaranteed to be the same as the order in which they were defined in the original object.