![](/img/trans.png)
[英]how to _.flatten with a parent array and an array nested ? (lodash)
[英]flatten json nested array (lodash/deepdash)
我有一個嵌套的 json 數組,我需要將其展平並刪除鍵(onSale,newComing)。 下面是 2 個 json:我擁有什么以及我想要什么。 (Javascript-lodash-deepdash)
我有的:
[
{
"id": 1,
"model": "Google Pixel 3",
"image": "/src/assets/images/high-angle-app-learning-new-language-phone_23-2148293463.jpg",
"path": "/src/assets/images/high-angle-app-learning-new-language-phone_23-2148293463.jpg",
"price": 799,
"quantity": 2
},
{
"id": 2,
"model": "Samsung Note 9",
"image": "/src/assets/images/samsung-1283938__340.jpg",
"price": 999,
"quantity": 2
},
{
"newComing": [
{
"id": 10,
"model": "Samsung Galaxy A7 (2020)",
"image": "/src/assets/images/Samsung S7.jpg",
"price": 533,
"quantity": 2
},
{
"id": 12,
"model": "Samsung Galaxy A7 (2018)",
"path": "https://media.istockphoto.com/photos/new-ios-14-screen-iphone-apples-next-operating-system-for-its-to-be-picture-id1270790424?k=20&m=1270790424&s=612x612&w=0&h=p_1DGqUriFPLKnt1KNbtRwdxJkB0ozkCQ3zE6KZOde8=",
"image": "https://media.istockphoto.com/photos/new-ios-14-screen-iphone-apples-next-operating-system-for-its-to-be-picture-id1270790424?k=20&m=1270790424&s=612x612&w=0&h=p_1DGqUriFPLKnt1KNbtRwdxJkB0ozkCQ3zE6KZOde8=",
"price": 630,
"quantity": 2
}
]
},
{
"onSale": [
{
"id": 14,
"model": "Samsung Galaxy A7 (2018)",
"path": "https://fdn.gsmarena.com/imgroot/reviews/20/samsung-galaxy-s20-plus/lifestyle/-1024w2/gsmarena_002.jpg",
"image": "https://fdn.gsmarena.com/imgroot/reviews/20/samsung-galaxy-s20-plus/lifestyle/-1024w2/gsmarena_002.jpg",
"price": 533,
"quantity": 2
},
{
"id": 15,
"model": "Samsung Galaxy A7 (2018)",
"path": "https://www.phonefinity.net/wp-content/uploads/camon-17-blue.jpg",
"image": "https://www.phonefinity.net/wp-content/uploads/camon-17-blue.jpg",
"price": 533,
"quantity": 2
}
]
}
]
我想要的是:
[
{
"id": 1,
"model": "Google Pixel 3",
"image": "/src/assets/images/high-angle-app-learning-new-language-phone_23-2148293463.jpg",
"path": "/src/assets/images/high-angle-app-learning-new-language-phone_23-2148293463.jpg",
"price": 799,
"quantity": 2
},
{
"id": 2,
"model": "Samsung Note 9",
"image": "/src/assets/images/samsung-1283938__340.jpg",
"price": 999,
"quantity": 2
},
{
"id": 10,
"model": "Samsung Galaxy A7 (2020)",
"image": "/src/assets/images/Samsung S7.jpg",
"price": 533,
"quantity": 2
},
{
"id": 12,
"model": "Samsung Galaxy A7 (2018)",
"path": "https://media.istockphoto.com/photos/new-ios-14-screen-iphone-apples-next-operating-system-for-its-to-be-picture-id1270790424?k=20&m=1270790424&s=612x612&w=0&h=p_1DGqUriFPLKnt1KNbtRwdxJkB0ozkCQ3zE6KZOde8=",
"image": "https://media.istockphoto.com/photos/new-ios-14-screen-iphone-apples-next-operating-system-for-its-to-be-picture-id1270790424?k=20&m=1270790424&s=612x612&w=0&h=p_1DGqUriFPLKnt1KNbtRwdxJkB0ozkCQ3zE6KZOde8=",
"price": 630,
"quantity": 2
},
{
"id": 14,
"model": "Samsung Galaxy A7 (2018)",
"path": "https://fdn.gsmarena.com/imgroot/reviews/20/samsung-galaxy-s20-plus/lifestyle/-1024w2/gsmarena_002.jpg",
"image": "https://fdn.gsmarena.com/imgroot/reviews/20/samsung-galaxy-s20-plus/lifestyle/-1024w2/gsmarena_002.jpg",
"price": 533,
"quantity": 2
},
{
"id": 15,
"model": "Samsung Galaxy A7 (2018)",
"path": "https://www.phonefinity.net/wp-content/uploads/camon-17-blue.jpg",
"image": "https://www.phonefinity.net/wp-content/uploads/camon-17-blue.jpg",
"price": 533,
"quantity": 2
}
]
我一直在玩 _.reduce、_.flatmap 並且無法接近。 感謝幫助。
flatMap
會做。
const array = [ { "id": 1, "model": "Google Pixel 3", "image": "/src/assets/images/high-angle-app-learning-new-language-phone_23-2148293463.jpg", "path": "/src/assets/images/high-angle-app-learning-new-language-phone_23-2148293463.jpg", "price": 799, "quantity": 2 }, { "id": 2, "model": "Samsung Note 9", "image": "/src/assets/images/samsung-1283938__340.jpg", "price": 999, "quantity": 2 }, { "newComing": [ { "id": 10, "model": "Samsung Galaxy A7 (2020)", "image": "/src/assets/images/Samsung S7.jpg", "price": 533, "quantity": 2 }, { "id": 12, "model": "Samsung Galaxy A7 (2018)", "path": "https://media.istockphoto.com/photos/new-ios-14-screen-iphone-apples-next-operating-system-for-its-to-be-picture-id1270790424?k=20&m=1270790424&s=612x612&w=0&h=p_1DGqUriFPLKnt1KNbtRwdxJkB0ozkCQ3zE6KZOde8=", "image": "https://media.istockphoto.com/photos/new-ios-14-screen-iphone-apples-next-operating-system-for-its-to-be-picture-id1270790424?k=20&m=1270790424&s=612x612&w=0&h=p_1DGqUriFPLKnt1KNbtRwdxJkB0ozkCQ3zE6KZOde8=", "price": 630, "quantity": 2 } ] }, { "onSale": [ { "id": 14, "model": "Samsung Galaxy A7 (2018)", "path": "https://fdn.gsmarena.com/imgroot/reviews/20/samsung-galaxy-s20-plus/lifestyle/-1024w2/gsmarena_002.jpg", "image": "https://fdn.gsmarena.com/imgroot/reviews/20/samsung-galaxy-s20-plus/lifestyle/-1024w2/gsmarena_002.jpg", "price": 533, "quantity": 2 }, { "id": 15, "model": "Samsung Galaxy A7 (2018)", "path": "https://www.phonefinity.net/wp-content/uploads/camon-17-blue.jpg", "image": "https://www.phonefinity.net/wp-content/uploads/camon-17-blue.jpg", "price": 533, "quantity": 2 } ] } ]; const flatten = array.flatMap(({newComing, onSale, ...item}) => newComing || onSale || [item]); console.log(flatten); const lodash = _.flatMap(array, ({newComing, onSale, ...item}) => newComing || onSale || [item]); console.log(lodash);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.