Home > OS >  How to find and remove the object key and value in javascript?
How to find and remove the object key and value in javascript?

Time:05-27

I have two variables in array and object, need to remove the fields which is not matched from allowed fields. Here is array and object variables.

Actually, my requirement is need to create separate object with only allowed fields from response data.please suggest the solution.

let allowedFields = [
  "organization_id",
  "organization_name",
  "payload.offer.application_id",
  "payload.offer.user_id",
  "id",
  "candidate_id",
];

Here is object values is below

let data = {
  organization_id: 4002400004,
  organization_name: "Velocity Global Integration Sandbox",
  action: "offer_updated",
  payload: {
    offer: {
      id: 4524843004,
      application_id: 31948577004,
      user_id: 4123647004,
      version: 1,
      sent_on: null,
      resolved_at: "2022-05-19T06:21:25.084Z",
      start_date: "2022-05-17",
      notes: null,
      job_id: 4298940004,
      offer_status: "Accepted",
    },
    resume: {
      name: "manikandan",
    },
  },
};

Data object only have the fields which is exists in the allowedFields array, expecting output is below

let output = {
  organization_id: 4002400004,
  organization_name: "Velocity Global Integration Sandbox",
  payload: {
    offer: {
      application_id: 31948577004,
      user_id: 4123647004,
    },
  },
};

CodePudding user response:

  1. Loop over all the paths in the allowedPaths array.

  2. Split the path by . to obtain an array of keys.

  3. Then loop over the array of keys and for every key fill in the resData object using actual data object.

Refer to the solution for better understanding:

const 
  allowedPaths = [
    "organization_id",
    "organization_name",
    "payload.offer.application_id",
    "payload.offer.user_id",
    "id",
    "candidate_id",
  ],
  data = {
    organization_id: 4002400004,
    organization_name: "Velocity Global Integration Sandbox",
    action: "offer_updated",
    payload: {
      offer: {
        id: 4524843004,
        application_id: 31948577004,
        user_id: 4123647004,
        version: 1,
        sent_on: null,
        resolved_at: "2022-05-19T06:21:25.084Z",
        start_date: "2022-05-17",
        notes: null,
        job_id: 4298940004,
        offer_status: "Accepted",
      },
      resume: {
        name: "manikandan",
      },
    },
  };

function filterData(data, allowedPaths) {
  const resData = {};
  allowedPaths.forEach((path) => {
    const pathArr = path.split(".");
    let actualDataObj = data;
    let resultantObj = resData;
    let toDetach;
    for (let i = 0; i < pathArr.length; i  ) {
      const k = pathArr[i];
      if (!actualDataObj?.hasOwnProperty(k)) {
        if (toDetach) {
          delete toDetach.object[toDetach.key];
        }
        return;
      }
      if (i === pathArr.length - 1) {
        resultantObj[k] = actualDataObj[k];
      } else if (!resultantObj.hasOwnProperty(k)) {
        resultantObj[k] = {};
        if (!toDetach) {
          toDetach = { object: resultantObj, key: k };
        }
      }
      resultantObj = resultantObj[k];
      actualDataObj = actualDataObj[k];
    }
  });
  return resData;
}

console.log(filterData(data, allowedPaths));

Note: The solution above also tries to handle scenarios where invalid paths are present in the allowedPaths array. Here are some invalid paths:

  • "id"
  • "payload.offer.application_id.value": toDetach variable in the above solution is meant for handling these types of situations.
  • "foo.bar.baz"

Relevant documentations:

CodePudding user response:

Can you try this.

function CheckKey(obj, keyVal, allowedFields) {
    Object.keys(obj).forEach(function (key) {
        let keyValInternal = keyVal.length === 0 ? key : keyVal   '.'   key;

        if (typeof obj[key] === 'object') {
              return CheckKey(obj[key], keyValInternal, allowedFields);
        } else {
          if (allowedFields.indexOf(keyValInternal) < 0){
              delete obj[key];
          }  
        }
    });
}

I ran this on a simple set of data (example below) and it stripped out the "b" parameter

let x = {t: {a:1, b:2}, s: 3}

let allowedFields = [
  "t.a"
];

CheckKey(x, '', allowedFields);

console.log(x)

Output

{
    "t": {
        "a": 1
    }
}
  • Related