1

These are the Few arrays name:

var Transport = ['Bus', 'Car', 'Truck', 'Train'];
var Fruits = ['Apple', 'Banana', 'Grape'];
var Animals = ['Dog', 'Cat', 'Horse', 'Sheep'];

And I have a result set like this:

var x = [
            {'name': 'Bus'},
            {'name': 'Banana'},
            {'name': 'Car'},
            {'name': 'Dog'},
            {'name': 'Truck'},
            {'name': 'Cat'}
          ];

Now what I want Some logic which return That which element is lies on which category. Example is like this

//what I want: 
var newresultTransport = ['Bus', 'Car', 'Truck'];
var newresultAnimal = ['Dog', 'Cat'];
var newresultFruits = ['Banana'];

Any Help is apprecited

3 Answers 3

3

You could use an object for the groups, for the single values and for the result. Then generate a hash table to get the group for every name and use it for creating a group in the result set and push the name to this group.

var groups = { transport: ['Bus', 'Car', 'Truck', 'Train'], fruits: ['Apple', 'Banana', 'Grape'], animals: ['Dog', 'Cat', 'Horse', 'Sheep'] },
    array = [{ name: 'Bus' }, { name: 'Banana' }, { name: 'Car' }, { name: 'Dog' }, { name: 'Truck' }, { name: 'Cat' }],
    hash = {},
    grouped = {};

Object.keys(groups).forEach(function (k) {
    groups[k].forEach(function (a) {
        hash[a] = k;
    });
});

array.forEach(function (o) {
    var k = hash[o.name];
    grouped[k] = grouped[k] || [];
    grouped[k].push(o.name);
});

console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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

1 Comment

Thank you so much Nina Scholz.
2

First transform x to an array of strings using map

x = x.map( s => s.name ) ;

Now use filter

newresultTransport = Transport.filter( s => x.indexOf( s ) != -1 );

Similarly for Fruits and animals

newresultAnimal = Fruits.filter( s => x.indexOf( s ) != -1 );
newresultFruits = Animals.filter( s => x.indexOf( s ) != -1 );

Demo

var Transport = ['Bus', 'Car', 'Truck', 'Train'];
var Fruits = ['Apple', 'Banana', 'Grape'];
var Animals = ['Dog', 'Cat', 'Horse', 'Sheep'];
var x = [
  {'name': 'Bus'},
  {'name': 'Banana'},
  {'name': 'Car'},
  {'name': 'Dog'},
  {'name': 'Truck'},
  {'name': 'Cat'}
];
x = x.map( s => s.name ) ;
var newresultTransport = Transport.filter( s => x.indexOf( s ) != -1 );
var newresultAnimal = Fruits.filter( s => x.indexOf( s ) != -1 );
var newresultFruits = Animals.filter( s => x.indexOf( s ) != -1 );
   
console.log( newresultTransport , newresultAnimal , newresultFruits  );

Comments

0

When creating var "x" in your question:

var x = [
{'name': 'Bus'},
{'name': 'Banana'},
{'name': 'Car'},
{'name': 'Dog'},
{'name': 'Truck'},
{'name': 'Cat'}
]

1) You left out the categories. You can can revise "x" to include a "category" property for each object.

var x = [
{'name': 'Bus', 'category':'transport'},
{'name': 'Banana', 'category':'fruit'},
{'name': 'Car', 'category':'transport'},
{'name': 'Dog', 'category':'animal'},
{'name': 'Truck', 'category':'transport'},
{'name': 'Cat', 'category':'animal'}
]

2) You can then use filter to filter the array down to a category desired. Example:

var x = [
{'name': 'Bus', 'category':'transport'},
{'name': 'Banana', 'category':'fruit'},
{'name': 'Car', 'category':'transport'},
{'name': 'Dog', 'category':'animal'},
{'name': 'Truck', 'category':'transport'},
{'name': 'Cat', 'category':'animal'}
]

const justTransport = x.filter(function(el){
    return el.category === "transport"
});

console.log(justTransport);

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.