我需要結合rows和rows2。如果其中任何一個都沒有值,則它們的值為“無”。我如何將它們結合起來?現在的問題是它創建了多個空行。
Codesandbox
uj5u.com熱心網友回復:
您可以使用以下語法result。只需將地圖回呼函式替換為您想要的任何內容:
const rows = [
{
name: "frozen yogurt",
calories: 22,
fat: 33,
carbs: 44,
protein: 44
},
// ...
];
const rows2 = "none";
const result = [...(Array.isArray(rows) ? rows : []), ...(Array.isArray(rows2) ? rows2 : [])]
.map(row => /* whatever you want here, but I'll use */ row);
console.log(result);
或者您可以創建一個函式并重新使用它:
function arrayValueOrEmptyArray (value) {
return Array.isArray(value) ? value : [];
}
const rows = [
{
name: "frozen yogurt",
calories: 22,
fat: 33,
carbs: 44,
protein: 44
},
// ...
];
const rows2 = "none";
const result = [...arrayValueOrEmptyArray(rows), ...arrayValueOrEmptyArray(rows2)].map(row => row);
console.log(result);
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/405720.html
標籤:
