如何根据另一个数组中的值来更新数组中的值?

问题描述 投票:0回答:3

我有两个数组是这样的。

let array1 = [
  {
    "id": 23,
    "name": "Telangana",
  }
]

这里我需要更新array2 color value inside properties 基于阵列1 numberOfProjects value inside latestMetric. 正如你可以看到,在两个数组中 stateId and id 相同,如果 numberOfProjects 值在 1 - 1000. 我需要更新 color 值为 1然后 numberOfProjects 数值范围为 1000 - 2000. 我需要更新 color 值为 2...等等。我不知道如何实现这一点。我试图映射这两个数组,并能够得到的ID的.但我不知道如何比较他们和更新的值。请帮助我。

javascript arrays reactjs lodash
3个回答
1
投票

你可以这样做

 let updatedArr2 = [];

function updateArr2(arr2values, colorValue) {
  let updatedProperties = { ...arr2values.properties, color: colorValue };
  arr2values.properties = updatedProperties;
  updatedArr2.push(arr2values);
}
array2.map(arr2values =>
  array1.map(arr1values => {
    if (arr2values.properties.stateId === arr1values.latestMetric.stateId) {
      if (
        arr1values.latestMetric.numberOfProjects >= 1 &&
        arr1values.latestMetric.numberOfProjects <= 1000
      ) {
        updateArr2(arr2values, 1);
      } else if (
        arr2values.latestMetric.numberOfProjects >= 1000 &&
        arr2values.latestMetric.numberOfProjects <= 2000
      ) {
        updateArr2(arr2values, 2);
      }
    }
  })
);

console.log(updatedArr2);

1
投票

你可以在 array1 然后检查是否有任何对象在 array2 吻合 stateId如果是,则检查项目数量在 array1 对象,并改变 color 的对象的 array2 一样 stateId,类似的东西。

array1.forEach((o) => {
  let matches = array2.filter(
    (o2) => o2.properties.stateId === o.latestMetric.stateId
  );
  let projects = o.latestMetric.numberOfProjects;
  for (let match of matches) {
    if (projects > 1 && projects < 1000) {
      match.properties.color = 1;
    } else if (projects >= 1000 && projects < 2000) {
      match.properties.color = 2;
    }
  }
});

let array1 = [
  {
    id: 23,
    name: "Telangana",
    code: "lnn",
    regionId: 1,
    isActive: true,
    latitude: 17.8495919,
    longitude: 79.1151663,
    latestMetric: {
      stateId: 23,
      year: 0,
      constructionValueInMn: 84623,
      constructionAreaInMnSqft: 32,
      numberOfProjects: 406,
      noOfCompletedProjects: 19,
      noOfOngoingProjects: 387,
      noOfUpcomingProjects: 0,
      growthRate: 0,
      averagePricePerSqftInRs: 0,
      totalAreaInMnSqft: 71,
      overAllAvgSqft: 0,
      eachVariantAvgSqft: 0,
      noOfTypeOfVariant: 0,
      projectCompletionCycle: 0,
    },
    createdAt: "2020-04-21T00:35:11.684134",
    updatedAt: "2020-04-21T00:35:11.684134",
  },
];

let array2 = [
  {
    type: "Feature",
    geometry: {
      type: "Polygon",
      coordinates: [
        [
          [77.19721, 28.861519],
          [77.203836, 28.86004],
        ],
      ],
    },
    properties: {
      cartodb_id: 26,
      state_code: 7,
      st_nm: "NCT of Delhi",
      color: 2,
      id: 23,
      stateId: 23,
    },
  },
];

array1.forEach((o) => {
  let matches = array2.filter(
    (o2) => o2.properties.stateId === o.latestMetric.stateId
  );
  let projects = o.latestMetric.numberOfProjects;
  for (let match of matches) {
    if (projects > 1 && projects < 1000) {
      match.properties.color = 1;
    } else if (projects >= 1000 && projects < 2000) {
      match.properties.color = 2;
    }
  }
});

console.log(array2);

1
投票

试试这个

array2.map(arr2 => {
    //Find to return the position when the id's are the same
    const arr1 = array1.find(arr => arr.latestMetric.stateId == arr2.properties.id)

    // If find was successful, do this
    if (arr1) {
        // Destructuring assignment to be easier to compare
        const { numberOfProjects } = arr1.latestMetric

        if (numberOfProjects >= 1 && numberOfProjects < 1000)
            arr2.properties.color = 1
        else if (numberOfProjects >= 1000 && numberOfProjects < 2000)
            arr2.properties.color = 2
    }
})
© www.soinside.com 2019 - 2024. All rights reserved.