簡體   English   中英

將對象推入嵌套在另一個數組中的數組中

[英]Object pushed into array nested within another array

我有一個對象數組,稱為specials ,每個對象看起來像這樣:

{
apr_rate:"",
client_id:"",
disclaimer:",
lease_payment: "",
make:"",
model:"",
name:"",
platform:"",
price:"",
specialOrder:"",
specialStyle: "",
trim:"",
visible:"",
website:"",
year:"",
}

我有一個循環遍歷數組中的每個對象,並檢查是否lease_payment屬性為空。 在這種情況下,我將特定對象從數組中拼接出來,將其存儲在名為tempObj的新對象中,然后將其推入名為tempArray的新數組中。 我進行拼接的原因是,我需要lease_payment以升序對這個對象數組進行排序-當沒有租賃付款時,它需要按price按升序對其余對象進行排序。 但是,價格的訂購需要租賃的訂購之后進行。 見下文:

    if (specials[i].lease_payment == "") {
        tempObj = specials.splice(i, 1);
        tempArray.push(tempObj);
    } else {
        // else just assume they're all lease offers and sort by lease payment
        specials.sort(function(a, b) {
            return a.lease_payment - b.lease_payment
        });
    }

然后,我檢查該新數組tempArray是否具有1個對象或多個對象。 如果只有1,我會立即將其推回到主要的specials數組中,該數組將位於后面; 也沒有另一個對象可以對其進行比較和排序。 如果有多個對象,我會根據價格的升序對這些對象進行重新排序,然后將它們分別推入specials數組,因為需要將它們視為自己的對象。 見下文。

if (tempArray.length == 1) {
    specials.push({tempArray});

} // else just sort the temp array by ascending price, push into main specials later
else if (tempArray.length > 1) {

    tempArray.sort(function(a, b) {
        return a.price - b.price
    });

    // grabs each individual object within temp array and pushes one at a time into main specials
    for (i = 0; i < tempArray.length; i++) {
        specials.push(tempArray[i]);
    }
}

發生的情況是,無論哪種情況,每當我將對象推回specials數組時,它都嵌套在另一個數組中,如以下屏幕快照所示: ss

在這種情況下,五個特殊對象中的兩個被取出,排序並放回去。但是,它們現在嵌套在一個數組中。

我是否缺少某些東西或做錯了什么? 任何幫助,將不勝感激。

發生這種情況是因為splice返回一個數組。 所以符合

tempObj = specials.splice(i, 1);

tempObj將是存儲此類的數組。 一個解決方案是,寫

tempObj = specials.splice(i, 1)[0];

您的推送重推有些復雜。 我會像這樣重新排序:

 specials.sort((a,b)=>{
   if(a.lease_payment && b.lease_payment){
    return a.lease_payment-b.lease_payment;//sort after lease payment
   }else if(a.lease_payment){
    return 1;//lease payment is already upper
   }else if(b.lease_payment){
    return -1;//swapp
   }else{
    //no leasepayment sort after prize;
   return a.price - b.price;
   }});

或簡稱:

specials.sort((a,b)=>a.lease_payment&&b.lease_payment?a.lease_payment-b.lease_payment:a.lease_payment?1:b.leasepayment?-1:a.price-b.price);

您使它更具可讀性:

var leased = specials.filter(function(e) { return e.lease_payment != ""; }).sort(function(a, b) { return b.price - a.price });

var notLeased = specials.filter(function(e) { return e.lease_payment == ""; }).sort(function(a, b) { return b.price - a.price });

specials = leased.concat(notLeased);

您只能使用排序功能,該功能首先按

  • lease_paymentlease_payment ,假設值是一個字符串
  • price ,升序,假設值是一個數字

 var data = [{ lease_payment: '', price: 30 }, { lease_payment: '', price: 10 }, { lease_payment: 'a', price: 11 }, { lease_payment: 'b', price: 13 }, { lease_payment: 'c', price: 15 }, { lease_payment: '', price: 8 }]; data.sort(function (a, b) { return a.lease_payment.localeCompare(b.lease_payment) || a.price - b.price; }); console.log(data); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM