1

I have a $.post jQuery call that calls a php file. The file then returns a JSON encoded array. Then, the array is mapped to edit some of the data in the array. However, I get the error arr.map is not function.

Here is the array being passed in the $.post call.

[{"set":"Alpha","key":"256"},
{"set":"Omega","key":"671"},
{"set":"Theta","key":"762"},
{"set":"Beta","key":"462"}]

Here is the map function.

idHash = {'Alpha': '1', 'Beta': '2', 'Theta': '3', 'Omega': '4'};
var arr = arr.map(function(item){
    item.set = idHash[item.set]
    return item;
})

After the map function, the array should look like this.

[{"set":"1","key":"256"},
{"set":"4","key":"671"},
{"set":"3","key":"762"},
{"set":"2","key":"462"}]

2 Answers 2

8

The array you are executing .map() would need to be a jquery object. If you set the result data equal to a jquery array with $ affixed to it you should able to use the jquery map utility. You can also use $.map(array, function(item){});,

https://jsfiddle.net/62v2a0cw/1/

idHash = {'Alpha': '1', 'Beta': '2', 'Theta': '3', 'Omega': '4'};

var arr = $.map(idHash, function(item){
    item.set = idHash[item.set];
    console.log(item);
    return item;
});

Please see jsFiddle link. Let me know if that helps. That is simply to assist with .map(). If you want to create an array of JSON objects, you could use jQuery .each() and push each key, value pair accordingly to an array. You could use manipulate the value on each iteration accordingly to your needs. Based on your comments, assuming the JSON you are working with identified by idHash is equal to {'Alpha': '1', 'Beta': '2', 'Theta': '3', 'Omega': '4'}

arr = [];
idHash = {'Alpha': '1', 'Beta': '2', 'Theta': '3', 'Omega': '4'};

$.each(idHash, function(key, value){
    // do whatever with the value before setting
    value *= 15;
    arr.push({"set": key, "key": value });
});    
console.log(arr);

https://jsfiddle.net/62v2a0cw/2/

Sign up to request clarification or add additional context in comments.

5 Comments

I get this error. TypeError: Cannot use 'in' operator to search for '2113' in and then it lists the array.
What is idHash referring to? If you replace that with a console.log(item); the .map() works.
.map() requires either a jquery object using the $ or to call it such as $.map(array, function(){});. The error may be just with the item.set line of code.
idHash = {'Alpha': '1', 'Beta': '2', 'Theta': '3', 'Omega': '4'};
The idHash is supposed to be updating the values under the set key in this array. [{"set":"Alpha","key":"256"}, {"set":"Omega","key":"671"}, {"set":"Theta","key":"762"}, {"set":"Beta","key":"462"}]
1

Signature of .map() is:

jQuery.map( array, callback )

So, you can try this:

var data = [{"set":"Alpha","key":"256"},
{"set":"Omega","key":"671"},
{"set":"Theta","key":"762"},
{"set":"Beta","key":"462"}];

$.map(data, function(elem){
    elem.set = newValue;
    elem.key = newValue;
});

Here is the fiddle.

1 Comment

This was the short, elegant answer I was hoping to find! Everyone else comes up with these convoluted explanations, but your example worked exactly as I needed it to. Thank you so much.

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.