Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
81
rated 0 times [  84] [ 3]  / answers: 1 / hits: 14266  / 11 Years ago, thu, january 2, 2014, 12:00:00

I'm trying to create a Dictionary with a key and a value list pair. I'm able to create a dictionary for a key value pair but I need to insert a list of Items as value for a key value.
Here is my approach:



keys = ['A', 'B', 'C'];
Elements Corresponding to 'A' : 'apple'
Elements Corresponding to 'B' : 'ball', 'balloon','bear'
Elements Corresponding to 'C' : 'cat','cow'


and my result should be like:



{ key:'A' value:['apple'], key:'B' value:['ball',balloon','bear'], Key:C' value:['cat','cow']}


Here is just a sample data, I will get data dynamically from a table.Please help me out.Thanks In advance.


More From » jquery

 Answers
7

This code can add a new key-value pair into some dictonary like object.



var dictionary= {};
function insertIntoDic(key, value) {
// If key is not initialized or some bad structure
if (!dictionary[key] || !(dictionary[key] instanceof Array)) {
dictionary[key] = [];
}
// All arguments, exept first push as valuses to the dictonary
dictionary[key] = dictionary[key].concat(Array.prototype.slice.call(arguments, 1));
return dictionary;
}

[#49059] Wednesday, January 1, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;