简体   繁体   中英

How to safely wrap an object into an array in JavaScript

I have a function that takes in an Array , iterates over it finding all Objects and displays them to the UI.

In rare cases, I have to supply an Object (result from a WS as application/JSON ) which is not an Array by default and hence my function fails to iterate over it and display on the UI.

In normal cases my Array looks like this:

[
  { "name" : "foo"},
  { "name" : "bar"},
  { "name" : "baz"}
]

and this works like it is supposed to. However, sometimes the data I get could be this:

{ "name" : "I am not in a List"}

and my function that takes in an array looks like this:

function loadJSONIntoUI(data) {

    for (var aMsg = 0; aMsg < data.length(); aMsg++) {
        // Do something with each `index` in the List
    }
}

Is there a way I can detect that the single object which is not an array is an odd one and probably put it into a List on the fly and pass it to a function?

So far I have tried to use typeof and also tried to create a new Array on the fly and push my object into it but it prints out a 1 when I do that.

A one liner:

[couldBeArray].flat()

Examples:

const anObj = {name: "Hi"};
const anArr = [{name: "Hi"}];

const wrapped1 = [anObj].flat() // wrapped1 is [{name: "Hi"}]
const wrapped2 = [anArr].flat() // wrapped2 is [{name: "Hi"}]

You can simply use Array.concat() to auto-wrap an object into an array:

 const obj = {name: "foo"}; const arr = [{name: "bar"}]; const result1 = [].concat(obj); // result1 is [{name: "foo"}] const result2 = [].concat(arr); // result2 is [{name: "bar"}] console.log(result1) console.log(result2)

you can transform it in an array if is not and let iterate one time:

function loadJSONIntoUI(data) {

    if(!(data instanceof Array)){
       data = [data];
    }

    for (var aMsg = 0; aMsg < data.length; aMsg++) {
        // Do something with each `index` in the List
    }
}

Also, length doesn't need to be called like a method.

Let me know if it works

Cheers

Array.isArray can be used to achieve what you need:

function loadJSONIntoUI(data) {
    if(!Array.isArray(data)) {
        data = [data];
    }
    for (var aMsg = 0; aMsg < data.length(); aMsg++) {
        // Do something with each `index` in the List
    }
}

You need to check for an array and fix an error - should be just data.length , no brackets. See code below, check demo - https://fiddle.jshell.net/ermakovnikolay/fgedaubm/

function loadJSONIntoUI(data) {
    var data = Array.isArray(data) ? data : [ data ];
    for (var aMsg = 0; aMsg < data.length; aMsg++) {
        // Do something with each `index` in the List
        console.log(data[aMsg]);
    }
}

You can use Array.of(). So in your case Array.of(data) returns [{ "name" : "I am not in a List"}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM