比较对象与对象

时间:2018-04-09 09:37:56

标签: javascript object compare

我有简单对象的那个对象

public positions = {
    reading: {
        left: "0px",
    },
    writing: {
        left: "0px",
    },
    speaking: {
        left: "0px",
    },
    listening: {
        left: "0px",
    },
};

我需要检查是否有一些'离开'属性是相同的,如果他们是添加'顶部'其中一个属性。例如,如果我有这样的对象:

public positions = {
    reading: {
        left: "10px",
    },
    writing: {
        left: "20px",
    },
    speaking: {
        left: "20px",
    },
    listening: {
        left: "7px",
    },
};

Sholud转变为:

public positions = {
    reading: {
        left: "10px",
    },
    writing: {
        left: "20px",
    },
    speaking: {
        left: "20px",
        top: "10px"

    },
    listening: {
        left: "7px",
    },
};

我需要最简单的方法来做到这一点。

1 个答案:

答案 0 :(得分:0)

也许这个?

const positions = {
    reading: {
        left: "0px",
    },
    writing: {
        left: "0px",
    },
    speaking: {
        left: "0px",
    },
    listening: {
        left: "0px",
    },
};

const positions2 = {
    reading: {
        left: "0px",
    },
    writing: {
        left: "0px",
    },
    speaking: {
        left: "0px",
    },
    listening: {
        left: "0px",
    },
};

for (var key in positions) {
  if (positions2[key].left == positions[key].left) {
    positions2[key].top = '10px'
  }
}

console.log(positions2)