jQuery data.map 不是函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30803168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
data.map is not a function
提问by mrcat
I'm bashing my head against an error I can't work out how to fix. I have the following;
我正在为一个我无法解决的错误而烦恼。我有以下内容;
JSON
JSON
{"products":
[
{
"product_id" : "123",
"product_data" : {
"image_id" : "1234",
"text" : "foo",
"link" : "bar",
"image_url" : "baz"
}
},{
"product_id" : "456",
"product_data" : {
"image_id" : "1234",
"text" : "foo",
"link" : "bar",
"image_url" : "baz"
}
}
]}
and the following jQuery
和以下 jQuery
function getData(data) {
this.productID = data.product_id;
this.productData = data.product_data;
this.imageID = data.product_data.image_id;
this.text = data.product_data.text;
this.link = data.product_data.link;
this.imageUrl = data.product_data.image_url;
}
$.getJSON("json/products.json").done(function (data) {
var allProducts = data.map(function (item) {
return new getData(item);
});
});
yet I'm getting an error that map.data is undefined as a function? Looking at it I don't know what's not working as I've copied this to a new project from previously used code. The only thing different is the JSON source. The previous one didn't have the {"products":
part before the [] brackets. Is this what's throwing me off?
但我收到一个错误,map.data 未定义为函数?看着它,我不知道什么不起作用,因为我已将其从以前使用的代码复制到新项目中。唯一不同的是 JSON 源。前一个没有{"products":
[] 括号之前的部分。这是让我失望的原因吗?
回答by Henrik Andersson
Objects, {}
, in JavaScript does not have the method .map()
, it's only for Arrays, []
.
Objects, {}
, 在 JavaScript 中没有这个方法.map()
,它只用于数组,[]
。
So in order for your code to work change data.map()
to data.products.map()
since products
is an array which you can iterate upon.
因此,为了让您的代码工作,更改data.map()
为data.products.map()
因为products
是一个您可以迭代的数组。
回答by user3930374
The right way to iterate over objects is
迭代对象的正确方法是
Object.keys(someObject).map(function(item)...
Object.keys(someObject).forEach(function(item)...;
// ES way
Object.keys(data).map(item => {...});
Object.keys(data).forEach(item => {...});
回答by Arun P Johny
data
is not an array, it is an object with an array of products so iterate over data.products
data
不是一个数组,它是一个包含一系列产品的对象,所以迭代 data.products
var allProducts = data.products.map(function (item) {
return new getData(item);
});
回答by William Hou
The SIMPLESTanswer is to put "data"into a pair of square brackets (i.e. [data]):
最简单的答案是将“数据”放入一对方括号中(即[data]):
$.getJSON("json/products.json").done(function (data) {
var allProducts = [data].map(function (item) {
return new getData(item);
});
});
Here, [data]is an array, and the ".map"method can be used on it. It works for me!
回答by Bill King
If you want to map an object you can use Lodash. Just make sure it's installed via NPM or Yarn and import it.
如果你想映射一个对象,你可以使用 Lodash。只需确保它是通过 NPM 或 Yarn 安装并导入的。
With Lodash:
使用洛达什:
Lodash provides a function _.mapValues
to map the values and preserve the keys.
Lodash 提供了一个函数_.mapValues
来映射值并保留键。
_.mapValues({ one: 1, two: 2, three: 3 }, function (v) { return v * 3; });
// => { one: 3, two: 6, three: 9 }
回答by Kenlly Acosta
dataneeds to be Json object, to do so please make sure the follow:
数据需要是 Json 对象,为此请确保以下内容:
data = $.parseJSON(data);
Now you can do something like:
现在您可以执行以下操作:
data.map(function (...) {
...
});
I hope this help some one
我希望这对某人有所帮助
回答by Fernando Betancourt
this.$http.get('https://pokeapi.co/api/v2/pokemon')
.then(response => {
if(response.status === 200)
{
this.usuarios = response.data.results.map(usuario => {
return { name: usuario.name, url: usuario.url, captched: false } })
}
})
.catch( error => { console.log("Error al Cargar los Datos: " + error ) } )
回答by Daniel Ryan Snell
You can always do the following:
您始终可以执行以下操作:
const SomeCall = request.get(res => {
const Store = [];
Store.push(res.data);
Store.forEach(item => { DoSomethingNeat
});
});
回答by Giacomo Paita
There is an error on $.map()
invocation, try this:
$.map()
调用时出错,试试这个:
function getData(data) {
this.productID = data.product_id;
this.productData = data.product_data;
this.imageID = data.product_data.image_id;
this.text = data.product_data.text;
this.link = data.product_data.link;
this.imageUrl = data.product_data.image_url;
}
$.getJSON("json.json?sdfsdfg").done(function (data) {
var allPosts = $.map(data,function (item) {
for (var i = 0; i < item.length; i++) {
new getData(item[i]);
};
});
});
The error in your code was that you made return
in your AJAX call, so it executed only one time.
您的代码中的错误是您return
在 AJAX 调用中产生的,因此它只执行了一次。