1

I have simply object:

var page = [
    {
        title: 'aaa',
        text:  '111'
    },     
    {
        title: 'bbb',
        text:  '222'
    },     
    {
        title: 'ccc',
        text:  '333'
    },     
    {
        title: 'ddd',
        text:  '444'
    }, 
];

Data are examples.

And i have string:

var to_get = '222, 444';

There are texts separated by commas. How can i get Title for this text?

For this example i would receive:

var to_return = 'bbb, ddd';

Separated by commas.

I try use $.each and $.inArray but i dont know how to combine this for multi array.

jsfiddle

1 Answer 1

4

Like so (fiddle)

console.log('222, 444, bogus'.split(', ').reduce(function (p, c) {
    page.forEach(function (o) {
        if (o.text === c) p.push(o.title);
    });
    return p;
}, []).join(", "));
Sign up to request clarification or add additional context in comments.

2 Comments

Nice answer...love the chain.
thanks, this working ok, but if i enter not exist text this return error. jsfiddle.net/YrZNq/1 is possible to eliminate this error?

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.