使用键和删除尾随对象的键删除对象对象中的键

时间:2017-11-02 08:32:04

标签: javascript object ecmascript-6

用文字解释这很复杂,所以让我告诉你我从那里得到的东西。

我有以下对象:

{
  1: { name: 'Initial Step 1', stepNumber: 1 },
  2: { name: 'Initial Step 2', stepNumber: 2 },
  3: { name: 'Initial Step 3', stepNumber: 3 },
  4: { name: 'Initial Step 4', stepNumber: 4 },
}

我想为此步骤执行删除功能,然后递减尾随键,以便获得以下结果:

{
  1: { name: 'Initial Step 1', stepNumber: 1 },
  2: { name: 'Initial Step 3', stepNumber: 2 },
  3: { name: 'Initial Step 4', stepNumber: 3 },
};

我不确定实施deleteStep功能的最佳方式。

我已按照以下方式实施:

export const deleteStep = (stepNumber, steps) => {
    // What should I do here?
    return steps;
};

谢谢!

4 个答案:

答案 0 :(得分:4)

您可以删除实际密钥并检查是否存在更多密钥,然后创建新属性并删除旧参考。

function deleteAndShift(object, key) {
    delete object[key];
    while (++key in object) {
        object[key].stepNumber--;
        object[key - 1] = object[key];
        delete object[key];
    }
}

var object = { 1: { name: 'Initial Step 1', stepNumber: 1 }, 2: { name: 'Initial Step 2', stepNumber: 2 }, 3: { name: 'Initial Step 3', stepNumber: 3 }, 4: { name: 'Initial Step 4', stepNumber: 4 } };

deleteAndShift(object, 2);

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:2)

如果您可以/想要返回一个新对象,您可以执行以下操作:



const deleteStep = (stepNumber, steps) => {
    const result = {};
    Object.keys(steps).forEach(key => {
      key = Number(key);
      if (key < stepNumber) {
        // keep preceding entries
        result[key] = steps[key];
      } else if (key > stepNumber) {
        // shift following entries
        result[key - 1] = steps[key]; // or shallow copy with `Object.assign({}, steps[key])`
        result[key - 1].stepNumber--;
      }
    });
    return result;
};

const steps = {
  1: { name: 'Initial Step 1', stepNumber: 1 },
  2: { name: 'Initial Step 2', stepNumber: 2 },
  3: { name: 'Initial Step 3', stepNumber: 3 },
  4: { name: 'Initial Step 4', stepNumber: 4 },
};

console.log(deleteStep(3, steps));
&#13;
&#13;
&#13;

答案 2 :(得分:1)

你可以去不可变路线并创建一个像这样的新对象:

&#13;
&#13;
function omitStep (steps, stepNumber) {
  return Object.keys(steps).reduce(function (acc, key) {
    var keyNum = Number(key);
    if (keyNum < stepNumber) { 
      acc[key] = steps[key]
    } else if (keyNum > stepNumber) {
      acc[keyNum - 1] = steps[key];
    }
    
    return acc;
  }, {});
}

var newSteps = omitStep({
  1: { name: 'Initial Step 1', stepNumber: 1 },
  2: { name: 'Initial Step 2', stepNumber: 2 },
  3: { name: 'Initial Step 3', stepNumber: 3 },
  4: { name: 'Initial Step 4', stepNumber: 4 },
}, 3);

console.log(newSteps);
&#13;
&#13;
&#13;

答案 3 :(得分:1)

一种可能的解决方案是使用reduce
reduce的优点是您返回一个新对象,而您的旧对象未被修改。

const removeStep = (steps, stepNumber) => {
  return Object.keys(steps).reduce((newSteps, current) => {
    const numberOfNewStep = Object.keys(newSteps).length + 1
    return Number(current) === stepNumber
      ? newSteps
      : Object.assign(
        {},
        newSteps, 
        { [numberOfNewStep]: Object.assign(
            {}, 
            steps[current], 
            {stepNumber: numberOfNewStep}
          )
        }
      )
  }, {})
}

console.log(removeStep({
  1: { name: 'Initial Step 1', stepNumber: 1 },
  2: { name: 'Initial Step 2', stepNumber: 2 },
  3: { name: 'Initial Step 3', stepNumber: 3 },
  4: { name: 'Initial Step 4', stepNumber: 4 },
}, 1))