简体   繁体   中英

How do I get just a new list of numbers from a json array using node.js

Given this json object

{
    "contacts": [
        {
            "name": "Jane doe",
            "numbers": [
                {
                    "number": "07123456789"
                }
            ],
            "groups": [
                {
                    "group": "Everyone"
                },
                {
                    "group": "Exec"
                }
            ]
        }, 
        {
            "name": "John Smith",
            "numbers": [
                {
                    "number": "07987654321"
                }
            ],
            "groups": [
                {
                    "group": "Everyone"
                }
            ]
        }
    ]
}

How do I return a new array of telephone numbers where the group equals a given value.

So, for example, I'd expect a single array of "07123456789" where the contact group = "Exec"

Also, where the Group = "Everyone" i'd expect an array of 2 items: "07123456789" and "07987654321"

I've tried:

 var data = require('../data/data.json')

 var peopleInGroup = data.contacts.filter(function (value) {
     value.groups.filter(function (type) {
         return type.group === recipients[i];
     })
 });

recipients is an array of groups "Everyone, "Exec", "Overseas"

Following this post i changed the second most upvoted answer code a bit to have this:

const getContactsByGroupName = (groupName) => {
    return a.contacts
        .filter((element) =>
            element.groups.some((subElement) => subElement.group === groupName))
        .map(element => {
            return Object.assign({}, element, { group: element.groups.filter(subElement => subElement.group === groupName) });
        });
}

console.log(getContactsByGroupName("Exec"))

This should do the trick. the variable a here is of course your json object name

The following can do what you want with simply using the Array prototype methods:

function filterByGroup(obj, val) {

    const objArray = obj.contacts.filter((innerObj, index, array) => {
            a = innerObj.groups.filter(group => group.group === val)
            return a.length > 0 ? innerObj : false;
    });

    return (objArray.map(obj => obj.numbers.map( number =>number.number))).flat();

}

You can then call it:

filterByGroup(obj, 'Exec')

Where obj is your initial object!

Here is piece of code which creates a JSON which has three groups of numbers:

final_data = {"Everyone": [], "Exec": [], "Overseas": [] }
data.contacts.forEach( (contact) => {
    contact.groups.forEach(group => {
        final_data[group.group].push(contact.numbers);
    })
})

console.log(final_data);

You could simply run trough every entry of contacts and generate an output-object containing arrays of numbers for each group. Here is an example.

 var input_json = { "contacts": [ { "name": "Jane doe", "numbers": [ { "number": "07123456789" } ], "groups": [ { "group": "Everyone" }, { "group": "Exec" } ] }, { "name": "John Smith", "numbers": [ { "number": "07987654321" } ], "groups": [ { "group": "Everyone" } ] } ] } //define the output objects that will contain the number-arrays by group-key var output = {} //run trough every contact for (let i in input_json.contacts) //run trough every group of current contact for (let j in input_json.contacts[i].groups) //run trough every contact-number for current contact-group for (let k in input_json.contacts[i].numbers) //if group-key already exists in output -> push number in array if (output[input_json.contacts[i].groups[j].group]) output[input_json.contacts[i].groups[j].group].push (input_json.contacts[i].numbers[k].number) //if group-key does not exist -> add new key-array pair else output[input_json.contacts[i].groups[j].group] = [input_json.contacts[i].numbers[k].number] //write output as JSON to document document.write (JSON.stringify (output))

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