How to use the zipObject function from lodash

Find comprehensive JavaScript lodash.zipObject code examples handpicked from public code repositorys.

5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
 * @param {Array} [values=[]] The array of values.
 * @returns {Object} Returns an object composed of the given keys and
 *  corresponding values.
 * @example
 *
 * _.zipObject(['fred', 'barney'], [30, 40]);
 * // => { 'fred': 30, 'barney': 40 }
 */
function zipObject(keys, values) {
  var index = -1,
fork icon73
star icon711
watch icon29

438
439
440
441
442
443
444
445
module.exports.wrap                = _.wrap;
module.exports.xor                 = _.xor;
module.exports.xorBy               = _.xorBy;
module.exports.xorWith             = _.xorWith;
module.exports.zip                 = _.zip;
module.exports.zipObject           = _.zipObject;
module.exports.zipObjectDeep       = _.zipObjectDeep;
module.exports.zipWith             = _.zipWith;
fork icon19
star icon122
watch icon0

+ 92 other calls in file

189
190
191
192
193
194
195
196
197
198
199
200
201
console.log(xorWith); // => [1.2, 3.4]


const zip = _.zip(['a', 'b'], [1, 2], [true, false]);
console.log(zip); // => [['a', 1, true], ['b', 2, false]]


const zipObject = _.zipObject(['a', 'b'], [1, 2]);
console.log(zipObject); // => { 'a': 1, 'b': 2 }


const zipObjectDeep = _.zipObjectDeep(['a.b[0].c', 'a.b[1].d'], [1, 2]);
console.log(zipObjectDeep); // => { 'a': { 'b': [{ 'c': 1 }, { 'd': 2 }] } }
fork icon0
star icon4
watch icon0

+ 15 other calls in file

167
168
169
170
171
172
173
174
175
176
  const parentCids = categoriesData.filter(c => c && c.parentCid).map(c => c.parentCid);
  if (!parentCids.length) {
    return cids.map(() => null);
  }
  const parentData = await Categories.getCategoriesData(parentCids);
  const cidToParent = _.zipObject(parentCids, parentData);
  return categoriesData.map(category => cidToParent[category.parentCid]);
};
Categories.getChildren = async function (cids, uid) {
  const categoryData = await Categories.getCategoriesFields(cids, ['parentCid']);
fork icon1
star icon1
watch icon5

+ 3 other calls in file

90
91
92
93
94
95
96
97
98
99
  }
});
const cids = _.uniq(topicData.map(t => t && t.cid).filter(cid => parseInt(cid, 10)));
const getToRoot = async () => await Promise.all(cids.map(Categories.getParentCids));
const [toRoot, teasers] = await Promise.all([getToRoot(), topics.getTeasers(topicData, uid)]);
const cidToRoot = _.zipObject(cids, toRoot);
teasers.forEach((teaser, index) => {
  if (teaser) {
    teaser.cid = topicData[index].cid;
    teaser.parentCids = cidToRoot[teaser.cid];
fork icon1
star icon1
watch icon5

72
73
74
75
76
77
78
79
80
81
82


const output = _(users) //в функцию lodash передаем массив данный с файла users
    .filter(u => (u.id > 20262535))//вывести больше чем 20262535
    .countBy(u => u.nickname) //ПОСЧИТАТЬ людей с одинаковыми никами и вывести эти значения
    .toPairs() //преобразовать в массивы
    .map(u => _.zipObject(['nickname', 'id'], u)) //добавить названия для значений
    .orderBy(u => u.nickname, 'desc') //отсортировать по убыванию
    .take(5)//ограничится 5 значениями
    .value()

fork icon0
star icon1
watch icon0

109
110
111
112
113
114
115
116
117
118
const pids = postData.map(post => post && post.pid);

async function getPostUserData(field, method) {
    const uids = _.uniq(postData.filter(p => p && parseInt(p[field], 10) >= 0).map(p => p[field]));
    const userData = await method(uids);
    return _.zipObject(uids, userData);
}
const [
    bookmarks,
    resolvedata,
fork icon1
star icon0
watch icon5

+ 2 other calls in file

18
19
20
21
22
23
24
25
26
27
28
29


//     let page = data.toString()
//     let defaultApp


//     let cookie = (req.headers.cookie) 
//         ? zipObject(
//             req.headers.cookie.split(";").map(d => {
//                 let r = d.split("=")
//                 return [r[0].trim(), r[1]]
//             })
fork icon0
star icon0
watch icon1

+ 11 other calls in file

185
186
187
188
189
190
191
192
193
194
const [ignoredCids, filtered] = await Promise.all([
    getIgnoredCids(),
    user.blocks.filter(uid, topicData),
]);

const isCidIgnored = _.zipObject(topicCids, ignoredCids);
topicData = filtered;

const cids = params.cids && params.cids.map(String);
tids = topicData.filter(t => (
fork icon0
star icon0
watch icon1

+ 7 other calls in file

138
139
140
141
142
143
144
145
146
147
const topicData = (await Topics.getTopicsFields(tids, ['tid', 'cid', 'uid', 'postcount', 'deleted', 'scheduled']))
    .filter(t => t.scheduled || !t.deleted);
const topicCids = _.uniq(topicData.map(topic => topic.cid)).filter(Boolean);

const categoryWatchState = await categories.getWatchState(topicCids, params.uid);
const userCidState = _.zipObject(topicCids, categoryWatchState);

const filterCids = params.cid && params.cid.map(cid => parseInt(cid, 10));

topicData.forEach((topic) => {
fork icon0
star icon0
watch icon1

+ 15 other calls in file

40
41
42
43
44
45
46
47
48
49
Topics.updateCategoryTagsCount = async function (cids, tags) {
    await Promise.all(cids.map(async (cid) => {
        const counts = await db.sortedSetsCard(
            tags.map(tag => `cid:${cid}:tag:${tag}:topics`)
        );
        const tagToCount = _.zipObject(tags, counts);
        const set = `cid:${cid}:tags`;

        const bulkAdd = tags.filter(tag => tagToCount[tag] > 0)
            .map(tag => [set, tagToCount[tag], tag]);
fork icon0
star icon0
watch icon1

+ 15 other calls in file

177
178
179
180
181
182
183
184
185
186

async function modifyUserData(users, requestedFields, fieldsToRemove) {
    let uidToSettings = {};
    if (meta.config.showFullnameAsDisplayName) {
        const uids = users.map(user => user.uid);
        uidToSettings = _.zipObject(uids, await db.getObjectsFields(
            uids.map(uid => `user:${uid}:settings`),
            ['showfullname']
        ));
    }
fork icon0
star icon0
watch icon1

+ 11 other calls in file

318
319
320
321
322
323
324
325
326
327
}

if (match[3]) {
  const values = match[3].split(',').map(part => part.trim());
  if (uniqueKey) {
    fields = _.zipObject(uniqueKey.fields, values);
  } else {
    fields[match[1]] = match[3];
  }
}
fork icon0
star icon0
watch icon0

337
338
339
340
341
342
343
344
345
346
  });
case '23505':
  // there are multiple different formats of error messages for this error code
  // this regex should check at least two
  if (errDetail && (match = errDetail.replace(/"/g, '').match(/Key \((.*?)\)=\((.*?)\)/))) {
    fields = _.zipObject(match[1].split(', '), match[2].split(', '));
    errors = [];
    message = 'Validation error';

    _.forOwn(fields, (value, field) => {
fork icon0
star icon0
watch icon0

+ 5 other calls in file

23
24
25
26
27
28
29
30
31
32
33
34


Thumbs.load = async function (topicData) {
	const topicsWithThumbs = topicData.filter(t => t && parseInt(t.numThumbs, 10) > 0);
	const tidsWithThumbs = topicsWithThumbs.map(t => t.tid);
	const thumbs = await Thumbs.get(tidsWithThumbs);
	const tidToThumbs = _.zipObject(tidsWithThumbs, thumbs);
	return topicData.map(t => (t && t.tid ? (tidToThumbs[t.tid] || []) : []));
};


Thumbs.get = async function (tids) {
fork icon0
star icon0
watch icon375

109
110
111
112
113
114
115
116
117
    title: {value: 'hello world'},
    timezone: {value: 'PST'},
    secondary_navigation: {value: false}
});

let values = _.zipObject(_.keys(publicSettings), _.fill(Array(_.size(publicSettings)), null));
values.title = 'hello world';
values.timezone = 'PST';
values.secondary_navigation = false;
fork icon0
star icon0
watch icon1

+ 4 other calls in file

80
81
82
83
84
85
86
87
88
    const { translationUnits, translationTargets, translationLanguages } = translationNodes;
    const keysArray = Array.from(translationUnits).map(unit => helpers_1.getTranslationKey(unit));
    const valuesArray = Array.from(translationTargets).map(target => helpers_1.getTranslationValue(target));
    const languageArray = Array.from(translationLanguages).map(lang => helpers_1.getTranslationLanguage(lang));
    config.language = languageArray[0];
    const content = lodash_1.zipObject(keysArray, valuesArray);
    return JSON.stringify(content, null, 2);
};
main(config.inputFile);
fork icon0
star icon0
watch icon1

119
120
121
122
123
124
125
126
127

	let uniqueGroups = _.uniq(_.flatten(members));
	uniqueGroups = Groups.removeEphemeralGroups(uniqueGroups);

	const isMembers = await Groups.isMemberOfGroups(uid, uniqueGroups);
	const isGroupMember = _.zipObject(uniqueGroups, isMembers);

	return members.map(groupNames => !!groupNames.find(name => isGroupMember[name]));
};
fork icon0
star icon0
watch icon1

165
166
167
168
169
170
171
172
173
174
175
176
		helpers.isAllowedTo(userPrivilegeList, uid, 0),
		user.isAdministrator(uid),
	]);


	const combined = userPrivileges.map(allowed => allowed || isAdministrator);
	const privData = _.zipObject(userPrivilegeList, combined);


	privData.superadmin = isAdministrator;
	return await plugins.hooks.fire('filter:privileges.admin.get', privData);
};
fork icon0
star icon0
watch icon1

106
107
108
109
110
111
112
113
114
115
});

return {
	topics,
	teasers,
	usersMap: _.zipObject(uids, users),
	categoriesMap: _.zipObject(cids, categoriesData),
	tidToGuestHandle: _.zipObject(guestTopics.map(t => t.tid), guestHandles),
	thumbs,
};
fork icon0
star icon0
watch icon1

+ 2 other calls in file

Other functions in lodash

Sorted by popularity

function icon

lodash.get is the most popular function in lodash (7670 examples)