1

I have this object:

{
mainTitle: "xx",
press1: {text: "xx", linkUrl: "xx", linkText: "xx"}
press1imgs: [{…}, {…}]
press2: {text: "xx", linkUrl: "xx", linkText: "xx"}
press2imgs: [{…}, {…}]
press3: {text: "xx", linkUrl: "xx", linkText: "xx"}
press3imgs: [{…}, {…}]
press4: {text: "xx", linkUrl: "xx", linkText: "xx"}
press4imgs: [{…}, {…}]
press5: {text: "xx", linkUrl: "xx", linkText: "xx"}
press5imgs: [{…}, {…}]
press6: {text: "xx", linkUrl: "xx", linkText: "xx"}
press6imgs: [{…}, {…}]
}

I'd like to reduce it down to an array but put all the press1, press1imgs, press2, press2Imgs in the same index. If the number after press is the same, put into the same index, so in total there'll be 6 indexes, to be like this:

[
{text: "xx", linkUrl: "xx", linkText: "xx", img: [{…}, {…}]},
{text: "xx", linkUrl: "xx", linkText: "xx", img: [{…}, {…}]},
{text: "xx", linkUrl: "xx", linkText: "xx", img: [{…}, {…}]},
{text: "xx", linkUrl: "xx", linkText: "xx", img: [{…}, {…}]},
{text: "xx", linkUrl: "xx", linkText: "xx", img: [{…}, {…}]},
{text: "xx", linkUrl: "xx", linkText: "xx", img: [{…}, {…}]},
]

I was able to convert the object into an array and get the number from pressx, but then I get lost into how to proceed, with reduce, filter, map? My attempt:

 const mapped = Object.keys(this.content).map((key) => ({
    key: key,
    value: this.content[key],
  }));

  console.log(mapped);
  mapped.map((item) => {
    console.log(item.key.charAt(5));
    if (item.key.charAt(5)){

    }
  });

1 Answer 1

1

Because the keys are pretty dynamic, I'm doubtful that the array methods are a good choice here. I'd use a loop:

const input = this.content;
const results = [];
let i = 1;
while (input.hasOwnProperty('press' + i)) {
  results.push({
    ...input['press' + i],
    img: input['press' + i + 'imgs'],
  });
  i++;
}

Live snippet:

const input = {
mainTitle: "xx",
press1: {text: "xx", linkUrl: "xx", linkText: "xx"},
press1imgs: [{}, {}],
press2: {text: "xx", linkUrl: "xx", linkText: "xx"},
press2imgs: [{}, {}],
press3: {text: "xx", linkUrl: "xx", linkText: "xx"},
press3imgs: [{}, {}],
press4: {text: "xx", linkUrl: "xx", linkText: "xx"},
press4imgs: [{}, {}],
press5: {text: "xx", linkUrl: "xx", linkText: "xx"},
press5imgs: [{}, {}],
press6: {text: "xx", linkUrl: "xx", linkText: "xx"},
press6imgs: [{}, {}]
}

const results = [];
let i = 1;
while (input.hasOwnProperty('press' + i)) {
  results.push({
    ...input['press' + i],
    img: input['press' + i + 'imgs'],
  });
  i++;
}
console.log(results);

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

2 Comments

This works but the original object may not always return the keys in order, so I was hoping to use the charAt(5) method.
Even if the object is ordered with keys 5, then 1, then 3, then 4, then 2, it should work fine - the i in the code in the answer increments up from 1 regardless, and pushes keys that exist.

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.