0

this is the array that i currently have

const myArr = [
    {
      "code": {
        "value": "AC16",
        "description": "text"
      },
      "convictionDate": {
        "value": "2019-03-07"
      }
    },
    {
      "code": {
        "value": "AC20",
        "description": "text"
      },
      "convictionDate": {
        "value": "2019-03-06"
      }
    }
  ];

I want to map over each nested object inside the array so that the value of each nested object is the value property of that object as a string. so the object would become:

const myArr = [
    {
      "code": "AC16",
      "convictionDate":"2019-03-07"
    },
    {
      "code":"AC20",
      "convictionDate": "2019-03-06"
    }
  ]

Ive tried this with no success:

const x = myArr.map((item)=>{
        console.log(item)
        Object.keys(item).map(function(key,i) {
            item[key] = item[key][value];
        })
    })
0

4 Answers 4

1

You are almost there. Just use array map

const myArr = [{
    "code": {
      "value": "AC16",
      "description": "text"
    },
    "convictionDate": {
      "value": "2019-03-07"
    }
  },
  {
    "code": {
      "value": "AC20",
      "description": "text"
    },
    "convictionDate": {
      "value": "2019-03-06"
    }
  }
];

let newArray = myArr.map(function(item) {
  return {
    code: item.code.value,
    convictionDate: item.convictionDate.value

  }

});
console.log(newArray)

Sign up to request clarification or add additional context in comments.

Comments

1

Map the array, and reduce the entries to the required object form:

const myArr = [{"code":{"value":"AC16","description":"text"},"convictionDate":{"value":"2019-03-07"}},{"code":{"value":"AC20","description":"text"},"convictionDate":{"value":"2019-03-06"}}];
  
const result = myArr.map(o =>
  Object.entries(o).reduce((r, [k, { value }]) => ({ ...r, [k]: value }), {})
);

console.log(result);

Comments

1

const myArr = [
    {
      "code": {
        "value": "AC16",
        "description": "text"
      },
      "convictionDate": {
        "value": "2019-03-07"
      }
    },
    {
      "code": {
        "value": "AC20",
        "description": "text"
      },
      "convictionDate": {
        "value": "2019-03-06"
      }
    }
  ];
  
let newArray = myArr.map(e => {
  return { "code":e.code.value, "convictionDate": e.convictionDate.value}
})

console.log(newArray);

Comments

0

It goes without saying there is some awesome reference material over at W3 Schools, the provide good detail on this in their article here

That said...

Why not simplify the code a little and try this against your array:-

const myArr = [
    {
      "code": {
        "value": "AC16",
        "description": "text"
      },
      "convictionDate": {
        "value": "2019-03-07"
      }
    },
    {
      "code": {
        "value": "AC20",
        "description": "text"
      },
      "convictionDate": {
        "value": "2019-03-06"
      }
    }
  ];

// PROPOSED SOLUTION CODE
const x = myArr.map((item)=>{
        return {code: item.code.value,convictionDate: item.convictionDate.value}
    });

// Your Desired Example Provided in Question
const xArr = [
    {
      "code": "AC16",
      "convictionDate":"2019-03-07"
    },
    {
      "code":"AC20",
      "convictionDate": "2019-03-06"
    }
  ];

// Output Validation
console.log('Response of Array \'x\'');
console.log(x);
console.log('Your Desired Example');
console.log(xArr);

When using 'map'; we must map the source array ('myArr') to our new target array ('x').

In the context of the example code you have provided, 'data.convictions' is undefined.

The magic is all here:-

const x = myArr.map((item)=>{
        return {code: item.code.value,convictionDate: item.convictionDate.value}
    });

Happy coding!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.