Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 785533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T20:53:40+00:00 2026-05-14T20:53:40+00:00

I have 3 json arrays, each with information listed in the same format: Array:

  • 0

I have 3 json arrays, each with information listed in the same format:

Array:
    ID:
    NAME:
    DATA:

    ID:
    NAME:
    DATA:

    etc...

My goal is to combine all 3 arrays into one array, and sort and display by NAME by passing the 3 arrays into a function.

The function I’ve tried is:

Javascript Call:

// to save time I'm just passing the name of the array, I've tried passing
// the full array name as json[0]['DATA'][array_1][0]['NAME'] as well.

combineNames(['array_1','array_2']);

FUNCTION:

function combineNames(names) {

    var allNames = []

    for (i=0;i<names.length;i++) {
        for (j=0;j<json[0]['DATA'][names[i]].length;j++) {
            allNames.push(json[0]['DATA'][names[i]][j]['NAME']);
        }
    }

    return allNames.sort();
}

The above gives me the error that NAME is null or undefined.

I’ve also tried using the array.concat function which works when I hard code it:

var names = [];
var allNames = [];

var names = names.concat(json[0]['DATA']['array_1'],json[0]['DATA']['array_2']);

for (i=0;i<names.length;i++) {
    allNames.push(names[i]['NAME']);
}

return allNames.sort();

But I can’t figure out how to pass in the arrays into the function (and if possible I would like to just pass in the array name part instead of the whole json[0][‘DATA’][‘array_name’] like I was trying to do in the first function…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T20:53:41+00:00Added an answer on May 14, 2026 at 8:53 pm

    If you’ve got 3 arrays like this:

    [{ "id":1, "name":"Bob", "data":1},{ "id":2, "name":"Fred", "data":2 }]
    

    Simply do:

    function combine() {
        var ar = [];
    
        return ar.concat.apply(ar, arguments).sort(function (a, b) {
            var aName = a.NAME;
            var bName = b.NAME;
            if (aName < bName) {
                return -1;
            } else if (aName == bName) {
                return 0;
            } else {
                return 1;
            };
        });
    };
    

    Then call it like:

    var jointArrays = combine(array1, array2, array3, ...);
    

    However, if your JSON looks like this:

    json[0]['DATA'][array_1]
    json[0]['DATA'][array_2]
    json[0]['DATA'][array_3]
    

    You can simply define combine() as follows, which will be more convenient:

    function combine(arrays) {
        var ar = [];
    
        return ar.concat.apply(ar, arrays).sort(function (a, b) {
            var aName = a.NAME;
            var bName = b.NAME;
            if (aName < bName) {
                return -1;
            } else if (aName == bName) {
                return 0;
            } else {
                return 1;
            };
        });
    };
    

    Then call it like:

    var jointArrays = combine(json[0].DATA);
    

    If you’re wanting an array of just the names, rather than the objects, use the following:

    function combine(arrays) {
        var ar = [],
            ret = [];
    
        ar = ar.concat.apply(ar, arrays);
    
        for (var i=0;i<ar.length;i++) {
            ret.push(ar.NAME);
        };
    
        return ret.sort();
    };
    

    Javascript is case sensitive; make sure it’s DATA and not data, and NAME and not name.

    Now for a little bit of housekeeping.

    In your example, both of your counter variables are being declared as “implied globals”, because you’re not prefixing them with the var statement (and implied globals are bad). You should use:

    for (var i=0;i<something.length;i++) {
       //
    };
    

    Instead of neglecting the var.

    Also, “{}” creates an object. “[]” creates an array. Javascript does not support associative array’s; e.g array’s with keys that are anything except a number. What you’re JSON is returning is an array of objects

    “Square notation” and “dot notation” are interchangeable. object["one"] is equivalent to object.one

    Square notation is generally used when the key is stored as a variable, or when you’re accessing an array.

    var key = "one";
    object[key]
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 421k
  • Answers 422k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer public class SomeClass { public void SomeMethod() { StackFrame frame… May 15, 2026 at 11:09 am
  • Editorial Team
    Editorial Team added an answer Is your last piece of code in the same file… May 15, 2026 at 11:09 am
  • Editorial Team
    Editorial Team added an answer dic .Select(kvp => new { Control = this.FindControl(kvp.Key), Visible =… May 15, 2026 at 11:09 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.