0

I have a JSON object that will sometimes be an object (a single instance) and sometimes be an array (multiple instances of the object). I need to write an if statement that basically says if this section of JSON is an object, wrap it in an array containing that single object, and if it's an array containing multiple objects, return that array. In either instance I'm returning an array containing either 1 or multiple objects. Here is what the JSON looks like when it is NOT an array.

"link": {
  "values": {
    "key1": "value1",
    ...
    "key8": "value8"
    },
  "key9": "value9"
  }

And it should look like this when it's an array:

"link": [{
  "values": {
    "key1": "value1",
    ...
    "key8": "value8",

    },
  "key9": "value9"
  }]

EDIT ----------------------------- This is what I've written so far that is producing the type error I'm experiencing.

  const isLinkArray = sections.values;
  isLinkArray.link = Array.isArray(isLinkArray.link) ? isLinkArray.link : [isLinkArray.link];

EDIT 2 ---------------------------

The final answer ended up being almost identical to Kinglish' answer, so I figured I would post it here. The issue I ran into was that the JSON right above 'link' was also an array and that was causing the typescript error.

  const sectionsWithLinkArray = sections.map((section) => {
      return {
        values: {
          ...section.values,
          link: !Array.isArray(section.values.link) ? [section.values.link] : section.values.link,
        },
      };
    }); 

3 Answers 3

2

You can use Array.isArray to check, then convert

let data = {
  "link": {
    "values": {
      "key1": "value1",
      "key8": "value8"
    },
    "key9": "value9"
  }
}

data.link = Array.isArray(data.link) ? data.link : [data.link];
console.log(data)

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

5 Comments

I get an error Property 'link' does not exist on type '() => IterableIterator<{ values: { link?:...
Which I'm confused by, because clearly link is there, it says so in the error message. Is it because link is an optional part of the object?
Can you show in your question the block of code in question (the iterator)?
See above under EDIT
I figured out the issue. The JSON had another array of objects above where I was working and that was causing the error.
0

This can be done by writing a simple function that checks if it's an array.

const returnArray = (value) => {
  if (Array.isArray(value) {
    return value;
  }
  return 
}

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

updated the answer of @Kinglish to typescript one because you cannot change types of defined value as it giving error for this either simply ignore the typescript or define types that simply accept link in object and array of object or just created new variable that expect a link in the array and wrap it inside data by simply doing this:

const data = {
  link: {
    values: {
      key1: 'value1',
      key8: 'value8',
    },
    key9: 'value9',
  },
};

// This is the type of data you can't change it by default and it doesn't expect array of object of `link`.
// const data: {
//   link: {
//     values: {
//       key1: string;
//       key8: string;
//     };
//     key9: string;
//   };
// };

const linkArray = { link: Array.isArray(data.link) ? data.link : [data.link] };

// Now this is the type of linkArray that expect array of object of `link`
// const linkArray: {
//   link: {
//     values: {
//       key1: string;
//       key8: string;
//     };
//     key9: string;
//   }[];
// };


console.log('data', data);
console.log('linkArray', linkArray);

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.