简体   繁体   English

将对象推入嵌套在另一个数组中的数组中

[英]Object pushed into array nested within another array

I have an array of objects, called specials , with each object looking like this: 我有一个对象数组,称为specials ,每个对象看起来像这样:

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

I have a loop going through each object in the array and checking to see if the lease_payment property is empty. 我有一个循环遍历数组中的每个对象,并检查是否lease_payment属性为空。 In the case that it is, I splice that particular object out of the array, store it in a new object called tempObj , and then push it into a new array called tempArray . 在这种情况下,我将特定对象从数组中拼接出来,将其存储在名为tempObj的新对象中,然后将其推入名为tempArray的新数组中。 The reason I am splicing is because I need to order this array of objects first by lease_payment in ascending order - when there is no lease payment, it needs to order the remaining objects by price in ascending order. 我进行拼接的原因是,我需要lease_payment以升序对这个对象数组进行排序-当没有租赁付款时,它需要按price按升序对其余对象进行排序。 However, the ordering of the price needs to happen after the ordering of lease. 但是,价格的订购需要租赁的订购之后进行。 See below: 见下文:

    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
        });
    }

I then check to see if that new array tempArray has 1 object, or multiple. 然后,我检查该新数组tempArray是否具有1个对象或多个对象。 If there is only 1, I immediately push it back into the main specials array, where it will be in the back; 如果只有1,我会立即将其推回到主要的specials数组中,该数组将位于后面; there is not another object to compare it too and order. 也没有另一个对象可以对其进行比较和排序。 If there is more than one object, I reorder those objects based on ascending price, followed by pushing them individually into the specials array, since they need to be seen as there own objects. 如果有多个对象,我会根据价格的升序对这些对象进行重新排序,然后将它们分别推入specials数组,因为需要将它们视为自己的对象。 See below. 见下文。

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]);
    }
}

What is happening is that in either case, whenever I push an object back into the specials array, it being nested within another array, as seen in this screenshot: 发生的情况是,无论哪种情况,每当我将对象推回specials数组时,它都嵌套在另一个数组中,如以下屏幕快照所示: ss

In this instance, two of the 5 specials objects were taken out, sorted, and put back in. However, they are now nested inside an array. 在这种情况下,五个特殊对象中的两个被取出,排序并放回去。但是,它们现在嵌套在一个数组中。

Am I missing something or doing something wrong? 我是否缺少某些东西或做错了什么? Any help would be appreciated. 任何帮助,将不胜感激。

It happens, because splice returns an array. 发生这种情况是因为splice返回一个数组。 So in line 所以符合

tempObj = specials.splice(i, 1);

tempObj will be an array an stored a such. tempObj将是存储此类的数组。 A solution would be, to write 一个解决方案是,写

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

Your push repush is a bit complex. 您的推送重推有些复杂。 I would reorder it like this: 我会像这样重新排序:

 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;
   }});

Or short: 或简称:

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);

you make it more readable: 您使它更具可读性:

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);

You could use only a sort function, which sorts first by 您只能使用排序功能,该功能首先按

  • lease_payment , asending, assuming the value is a string lease_paymentlease_payment ,假设值是一个字符串
  • price , ascending, assuming value is a number 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