let data = { "needData": [ { arrayData: [ { "key": "dummy", "value": "needed value" }, { "key": "secret", "random": "secret_random" }, { "key": "another", "value": "testing" }, { "key": "another_Secret", "random": "testing_value" }, ] } ] }; let json, testing; data.needData.map((value) => { json = { arrayData: [ { needed_key: value.arrayData[0].key, needed_value: value.arrayData[0].value, }, ], }; testing = { arrayData: [ { needed_key: value.arrayData.key, needed_value: value.arrayData.value, }, ], }; }); console.log("value got by running json", json); console.log("value got by running testing", testing); Estoy tratando de guardar datos en el objeto json usando la función de mapa, pero el problema es que puedo guardar datos usando needed_key:value.arrayData.key needed_value:value.arrayData.value
si toda la clave_necesaria y el valor_necesario son iguales, puedo usar el código anterior, pero aquí el problema es que el nombre de la clave es el mismo, pero el valor_necesario cambia random and value solo quiero guardar los valores de los datos que contienen un value no random , ¿hay alguna manera? puede obtener key and value valor, no key and random
Puedo usar el código manual para obtener los datos, pero ¿qué pasa si los datos son más y no quiero usar el código manual?
{ "arrayData": [ { "needed_key": "dummy", "needed_value": "needed value" }, { "needed_key": "another", "needed_value": "testing" } ] }Esto da su salida:
var output = { 'arrayData': data.needData[0].arrayData .filter(x => !Object.keys(x).includes('random')) .map(x => { return {needed_key: x['key'], needed_value: x['value']} }) } console.log(output);Implementación de Array.map y Array.filter .
let data = { "needData": [ { arrayData: [ { "key": "dummy", "value": "needed value" }, { "key": "secret", "random": "secret_random" }, { "key": "another", "value": "testing" }, { "key": "another_Secret", "random": "testing_value" }, ] } ] }; const output = data.needData.map((value) => { const { arrayData } = value; const opArrayData = arrayData.filter(item => item.key && item.value && !item.random); const parsedArray = opArrayData.map((node => ({ needed_key: node.key, needed_value: node.value }))); return { arrayData: parsedArray } }); console.log(output);