SDK
SDK

Reputation: 1508

How to update value in array based on values in another array?

I am having two array like this,

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

Here i need to update array2 color value inside properties based on array1 numberOfProjects value inside latestMetric. As u can see that in both arrays stateId and id are same.If numberOfProjects value is in the range 1 - 1000. I need to update the color value as 1. then numberOfProjects value is in the range 1000 - 2000. I need to update the color value as 2.so on. I dont know how to achieve this. I tried to map those two arrays and can able to get the ID's.But i dont know how to compare them and update the value . Pleas help me.Thanks in advance

Upvotes: 0

Views: 124

Answers (3)

João Ramos
João Ramos

Reputation: 11

Try this:

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

Upvotes: 1

Lu&#237;s Ramalho
Lu&#237;s Ramalho

Reputation: 10208

You could loop through each object in array1 and then check if there's any object in array2 that matches the stateId, if so, then check the number of projects in the array1 object and change the color of the object in array2 that has the same stateId, something like:

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

Upvotes: 1

Sohan Patil
Sohan Patil

Reputation: 170

You can do like this

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

Upvotes: 1

Related Questions