0

I have an object (values). I want to select the field which startWith(')and which contains an object(for this case the 2). I want to delete the entire object from the createValue array and place it in an object CreateFileValue. You can check the output

Example input:

const values = {
  ID: ,
  c: [{
      f: "",
      v: 'hello',
    },
    {
      f: "102",
      fi: "doc.pdf",
      v: {
        f: 'F2',
        fi: '',
        v: 'jkhkjhkhkjh'
      },
    }

  ]
}

Example output:

const values = {
  ID:817,
  c: [{
      f: "F",
      v: 'hello',
    }

  ],
  c: {
    "f": "F",
    "fi": "ff.pdf",
    "v": "jkhkjhkhkjh"
  }
}

const values = {
  ID: 7,
  c: [{
      f: "FL",
      v: 'hello',
    },
    {
      f: "F2",
      fi: "doc.pdf",
      v: {
        f: '',
        fi: '.pdf',
        v: 'jkhkjhkhkjh'
      },
    }

  ]
}

const res = () => {
  if (values.calues.startsWith('') && typeof values.cralues.startsWith('F') === 'object') {
    return {

    };

  }
}
};
console.log(res());
Marizona
  • 131
  • 1
  • 12
  • 1
    What have you tried so far? Please edit the question with a [mcve] – evolutionxbox Jul 21 '22 at 14:04
  • If your question and the answer contain sensitive code, you should edit to remove the sensitive part *without changing the meaning of the post*. So *do not* vandalize the entire post; only replace the sensitive part with something that's not sensitive. Then flag for moderator attention to ask a moderator to redact the revision history so that the sensitive code isn't visible there. See [What should I do if a user posts sensitive information as part of a question or answer?](https://meta.stackexchange.com/q/132117/349538) – Donald Duck Dec 07 '22 at 16:30
  • i need the post to be taken down, Any help on how to reach a team ? it is highly urgent – Marizona Dec 07 '22 at 16:45
  • @Alba from the link above, _Second, flag for moderator attention with the "in need of moderator intervention" flag, explaining why the sensitive information should be removed._ or delete the question altogether. – WOUNDEDStevenJones Dec 07 '22 at 20:03

3 Answers3

0

values.createValues.startsWith('FLD_STR_') isn't valid because createValues is an array. You'll need to iterate over values.createValues in order to check each field.startsWith('FLD_STR_') and if value is an object (see How to detect if a variable is a pure javascript object for an example of this).

const values = {
  ID: 748817,
  createValues: [{
      field: "FLD_STR_101",
      value: 'hello',
    },
    {
      field: "FLD_STR_102",
      fileName: "doc.pdf",
      value: {
        field: 'FLD_STR_102',
        fileName: 'bulletin_paie_avril.pdf',
        value: 'jkhkjhkhkjh'
      }
    }
  ]
}

const res = () => {
  var newResult = {
    ID: values.ID,
    createValues: []
  };
  
  values.createValues.forEach(function(item) {
    var newCreateValue = {};
    
    if (item.field.startsWith('FLD_STR_') && item.value.constructor.name === "Object") {
      newCreateValue.field = item.value.field;
      newCreateValue.value = item.value.value;
      newCreateValue.fileName = item.value.fileName;
      
      // if this is a createFilesValue, add it as a new key/value to the newResult object
      newResult.createFileValues = newCreateValue;
    } else {
      newCreateValue.field = item.field;
      newCreateValue.value = item.value;
      
      // if this is an existing createValues, add it to the createValues array
      newResult.createValues.push(newCreateValue);
    }
  });
  
  return newResult;
};
console.log(res());
WOUNDEDStevenJones
  • 5,150
  • 6
  • 41
  • 53
0

const values = {
  ID: 748817,
  createValues: [{
      field: "FLD_STR_101",
      value: 'hello',
    },
    {
      field: "FLD_STR_102",
      fileName: "doc.pdf",
      value: {
        field: 'FLD_STR_102',
        fileName: 'bulletin_paie_avril.pdf',
        value: 'jkhkjhkhkjh'
      },
    }

  ]
}

// build createFileValues structure
values.createFileValues = values.createValues.filter(v => v.field.startsWith("FLD_STR_") && typeof v.value === "object");
// filter the files out of createValues
values.createValues = values.createValues.filter(v => !values.createFileValues.includes(v));
console.log(values);
James
  • 20,957
  • 5
  • 26
  • 41
  • I do not want to return an array, createFileValue should be a simple object with – Marizona Jul 21 '22 at 14:32
  • Yeah, but you have an array of createValues, and I have no guarantee that there is only one element with a file. – James Jul 21 '22 at 14:41
  • basically createFileValue is an object, not an array. It will never be. also I just want to return the value value: { field: 'FLD_STR_102', fileName: 'bulletin_paie_avril.pdf', value: 'jkhkjhkhkjh' }, – Marizona Jul 21 '22 at 14:49
  • You can add `values.createFileValues = values.createFileValues[0].value` – James Jul 21 '22 at 15:15
0

This should work

const values = {
  ID: 748817,
  createValues: [{
      field: "FLD_STR_101",
      value: 'hello',
    },
    {
      field: "FLD_STR_102",
      fileName: "doc.pdf",
      value: {
        field: 'FLD_STR_102',
        fileName: 'bulletin_paie_avril.pdf',
        value: 'jkhkjhkhkjh'
      },
    }

  ]
}

const res = (data) => {
  let oldCreateValues = data.createValues;
  let oldCreateFileValues = data.createFileValues;
  
  let newCreateValues = [];
  let newCreateFileValues = oldCreateFileValues && Array.isArray(oldCreateFileValues) ? oldCreateFileValues : [];
  if (oldCreateValues && Array.isArray(oldCreateValues)) {
    oldCreateValues.forEach(item => {
        if (item.field.startsWith('FLD_STR_') && item.value && typeof item.value === 'object') {
        newCreateFileValues.push(item);
      } else {
        newCreateValues.push(item);
      }
    });
  }
  
  data.createValues = newCreateValues;
  data.createFileValues = newCreateFileValues;
  
  return data;
};

console.log(res(values));
Murali
  • 1
  • 2