2

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.

2
  • 1
    I don't really understand, it seems that you already answered your question. Just use an array as value for each key and append the items to the array. Commented Jan 2, 2014 at 12:38
  • I explained my problem with an example data... All I would get is a dynamic data. I don't even know how many elements would fall under a particular key value. Commented Jan 2, 2014 at 12:46

3 Answers 3

3

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

I've tried the same way but i'm getting only keys not the list items.
I make the error in code, so it not append the items, just create the keys. I update the code - current version work as expect. Sorry.
I tried out your solution but Didn't work for me. I made a JSFiddle for your reference Take a look jsfiddle.net/Harikrishna/ZX8ne/3
You miss the search conditions array and i swap the for loops the working code is here jsfiddle.net/kbS76 But if you try to use it in real app - not forget to add the code to exclude duplicate records if necessary.
Here I'm getting objects in list right.. How to access the values in that list...
|
2

Use a Json,

var xObj = {'A' : ['apple'] ,'B' : ['ball','balloon','bear'],'Ç' : ['cat','cow'] };

1 Comment

All I get is dynamic data.. I don't even know how many elements would be in a list. I will get a JSON Formatted data like {data:[{'key':'a',value:'apple'},{'key':'b',value:{'ball'},{'key':'b',value:'balloon'}]}
1

Here's an example:

/* Define dictionary */
var dict = {};

/* Define keys */
var keys = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

/* Assign array as value for each key */
for (var n = 0; n < keys.length; n++) {
    dict[keys[n]] = [];
}

/* Make up a bunch of words */
var words = ["apple", "ball", "balloon", "bear", "cat", "cow"];

/* Append these words to the dictionary according to their first letter */
for (n = 0; n < words.length; n++) {
    dict[words[n][0].toUpperCase()].push(words[n]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.