admin管理员组文章数量:1401365
How could I map a multidimensional array with differents keys? This is a example array similar: (my original array is obtained from ajax and PHP mysql query that's why I need to do this):
var products = [
{
id: 1,
name: 'John',
phones: {
sony:
{
brand: 'sony',
model: 'z3'
},
samsung:
{
brand: 'samsung',
model: 's7'
}
}
},
{
id: 2,
name: 'Mike',
phones: {
sony:
{
brand: 'sony',
model: 'z1'
},
nokia:
{
brand: 'nokia',
model: 'n8'
}
}
}
];
If I try to map this array I get: 'Uncaught TypeError: product.phones.map is not a function':
const List = ({ products, addToCart }) => {
return (
<div className="odds-3">
<div className="odds">
{products.map((product, index) =>
<div key={index}>
<p>{product.id} - {product.name}</p>
<ul>
{product.phones.map((phone, index) =>
<li key={index}>{phone.brand} - {phone.model}</li>
)}
</ul>
</div>
)}
</div>
</div>
);
}
With this array (without phones keys), works fine:
var products = [
{
id: 1,
name: 'John',
phones:
[{
brand: 'sony',
model: 'z3'
},
{
brand: 'samsung',
model: 's7'
}
]
},
{
id: 2,
name: 'Mike',
phones:
[{
brand: 'sony',
model: 'z1'
},
{
brand: 'nokia',
model: 'n8'
}
]
}
];
How could I map a multidimensional array with differents keys? This is a example array similar: (my original array is obtained from ajax and PHP mysql query that's why I need to do this):
var products = [
{
id: 1,
name: 'John',
phones: {
sony:
{
brand: 'sony',
model: 'z3'
},
samsung:
{
brand: 'samsung',
model: 's7'
}
}
},
{
id: 2,
name: 'Mike',
phones: {
sony:
{
brand: 'sony',
model: 'z1'
},
nokia:
{
brand: 'nokia',
model: 'n8'
}
}
}
];
If I try to map this array I get: 'Uncaught TypeError: product.phones.map is not a function':
const List = ({ products, addToCart }) => {
return (
<div className="odds-3">
<div className="odds">
{products.map((product, index) =>
<div key={index}>
<p>{product.id} - {product.name}</p>
<ul>
{product.phones.map((phone, index) =>
<li key={index}>{phone.brand} - {phone.model}</li>
)}
</ul>
</div>
)}
</div>
</div>
);
}
With this array (without phones keys), works fine:
var products = [
{
id: 1,
name: 'John',
phones:
[{
brand: 'sony',
model: 'z3'
},
{
brand: 'samsung',
model: 's7'
}
]
},
{
id: 2,
name: 'Mike',
phones:
[{
brand: 'sony',
model: 'z1'
},
{
brand: 'nokia',
model: 'n8'
}
]
}
];
Share
Improve this question
asked Oct 31, 2017 at 13:04
kurtkokurtko
2,1364 gold badges33 silver badges49 bronze badges
4 Answers
Reset to default 4products
isn't an array so the map
function won't work. Try
{Object.keys(product.phones).map((phone, index) =>
<li key={index}>{product.phones[phone].brand} - {product.phones[phone].model}</li>
)}
instead.
In your first example, the phones property is an object
not an array
and therefore does not have the map
function
The second one works phones
is an array
.
As others before me told you, products
isn't an array and therefore you can't use the map function on it.
Although, you can use the map
function from lodash
library which you can use in order to iterate over an object keys/values:
import map from 'lodash'
{map(product, (value, key) =>
...
)}
For more info, read here
Arrays work slightly differently in JavaScript. In your case, a drop-in fix would be iterating over the values of the object, something like this:
{products.map((product, index) =>
<div key={index}>
<p>{product.id} - {product.name}</p>
<ul>
{Object.values(product.phones).map((phone, index) =>
<li key={index}>{phone.brand} - {phone.model}</li>
)}
</ul>
</div>
)}
While some browsers still lack support for Object.values()
, the transpilation step should take care of this.
Note that it is generally not advisable to use indices as keys, as you might get weird rendering bugs should the ordering or the contents of your iterables change.
本文标签: javascriptReact Map multidimensional array with different keysStack Overflow
版权声明:本文标题:javascript - React: Map multidimensional array with different keys - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744299816a2599524.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论