数组拼接中的非唯一对象

时间:2018-07-22 15:14:44

标签: javascript

我正在尝试从两个现有数组中创建一个新数组。

  • 有一个totalStock变量,包含商店中的每个商品。
  • 有一个currentStock,其中包含当前库存中的物品
  • 现在我需要一个OutStock,其中包含没有库存的物品,问题是某些物品具有相同的名称,因为这会抛出错误的结果

在库存阵列中,有一些对象共享相同的id。以下代码的结果应该是一个包含id Metal和Wood对象的数组,但仅返回Metal。

 var totalStock = [
    {id:'Wood'}, {id:'Metal'}, {id:'Wood'}, {id:'Wood'}, {id:'Stone'}
];

var inStock = [
    {id:'Wood'}, {id:'Wood'}, {id:'Stone'} /* Metal is no longer in stock, nor is Wood */
];

var outStock = totalStock.filter(item => {
  return !inStock.some(inStockItem => inStockItem.id === item.id);
});

console.log(outStock); /* This only returns Metal but should also be returning wood */

JSFiddle

更新代码

var totalStock = [
    {id:'Wood'}, {id:'Metal'}, {id:'Wood'}, {id:'Wood'}, {id:'Stone'}
];

var inStock = [
    {id:'Wood'}, {id:'Wood'}, {id:'Stone'} /* Metal is no longer in stock, nor is Wood */
];

function calculateStock(stockArray){
    var totalStockAmount = {};
  stockArray.forEach(function(item){
    totalStockAmount[item.id] = 0;
  });

  stockArray.forEach(function(item){
    totalStockAmount[item.id] = totalStockAmount[item.id] + 1;
  });
  return totalStockAmount;
}

var totalStockAmount = calculateStock(totalStock),
        totalInStockAmount = calculateStock(inStock);

Object.keys(totalStockAmount).forEach(function(stock){
    if(!totalInStockAmount[stock]) totalInStockAmount[stock] = 0;
    totalInStockAmount[stock] = totalStockAmount[stock] - totalInStockAmount[stock];
 });

var noStockAmount = totalInStockAmount,
        noStock = [];

Object.keys(noStockAmount).forEach(function(stock){
    var amount = noStockAmount[stock];
  for(var x = 0; x < amount; x++){
    noStock.push({id:stock});
  }
});

console.log(noStock);

2 个答案:

答案 0 :(得分:0)

您需要对每个计数进行比较。

为inStock创建一个计数哈希图,类似于:

void MarkerItem::paint(QPainter *painter)
{
    qDebug() << " > MarkerItem paint, azimuth  " << miAzimuth;
    QPen pen;
    QBrush brush(mcColor);
    if (mbSelected)
    {
        pen.setColor(Qt::magenta);
        pen.setWidth(2);
        brush.setColor(mcColor.lighter(150));
    }
    else
    {
        pen.setColor(Qt::black);
        pen.setWidth(1);
    }

    painter->setPen(pen);
    painter->setBrush(brush);
    painter->setRenderHints(QPainter::Antialiasing, true);
    const QRectF rect = boundingRect();
    setAnchorPoint(QPointF(rect.center().x(),rect.bottom()));

    // we define a helper rectangle what we use to draw a pie,
    // as the drawPie() method expect a rect, and the pie will start from the
    // center of that rect
    QRectF cPieRect(anchorPoint().x()-rect.height(), anchorPoint().y()-rect.height(),
                    rect.height()*2,rect.height()*2);
    painter->drawPie(cPieRect,(90-miSectorSize/2)*16, miSectorSize * 16);

    // drawing the bounding rectangle in red for visual debugging
    QPen pen2(Qt::red,1);
    painter->setPen(pen2);
    painter->setBrush(QBrush(Qt::transparent));
    painter->drawRect(rect);
}

...    

void MarkerItem::setAzimuth(int angle)
{
    miAzimuth = angle;
    setTransformOriginPoint(anchorPoint());
    setRotation(miAzimuth);
}

然后您可以在对totalStock进行过滤时扣除,如果该属性不存在或为零,则表明它缺货

答案 1 :(得分:0)

我认为最终解决此问题的方法是使您的数据结构更有意义。我正在努力理解所有内容,但似乎ID是一种非唯一的键含义类型?而且我认为还有更多数据吗?缺货的意思是……我不能告诉你,因为你说伍德缺货,但是显然有2个库存。我猜那是...不同的木头?那么缺货是一系列商品,意味着至少有一个商品被出售了?我...该继续努力,但我可能误会了。

如果这些是不同的东西,则需要具有一些唯一的标识符。而且,那可能应该是ID。


我建议您使用一个包含所有产品的对象(可能用ID键入以便于检索),并仅使用ID来存储库存中/无库存的东西,这样就不会复制数据。

例如

var products = {
    1001: {
        id: 1001,
        type: 'wood',
        otherData: 'redwood',
    },
    1002: {
        id: 1002,
        type: 'wood',
        otherData: 'oak',
    },
    1003: {
        id: 1003,
        type: 'stone',
        otherData: 'limestone',
    },
    // etc
}


var totalStock = {
    1001: 4,
    1002: 4,
    1003: 6,
}

var inStock = {
    1001: 2,
    1002: 0,
    1003: 1,
}

var outStock = Object.keys(totalStock).filter(id => !inStock[id]); // [1002]

我想我对您希望每条数据的内容有误解,但是像这样对数据进行扁平化将为您提供更轻松的时间,并可能解决许多此类问题。