简体   繁体   中英

Extract all words from fields of objects inside array

i have this array:

array = [{"full_name":"louis jackson","title1":"english teacher,"description":"good and nice teacher"},{"full_name":"peter carey","title1":"math teacher,"description":"bad and ugly teacher"}]

i need to get this array

results = ["louis","jackson","english", "teacher",good","and","nice","teacher","peter","carey","math","teacher","bad","and","ugly","teacher"]

i have tried:

results = [];
array.forEach(item => {
results.push (item.full_name.split(" "));
results.push (item.title1.split(" "));
results.push (item.description.split(" "));    
}

i just get multiple separated arrays; can anyone guide me in right way?

I made a bit of clean code to do it all for you

 array = [{"full_name":"louis jackson","title1":"english teacher","description":"good and nice teacher"},{"full_name":"peter carey","title1":"math teacher","description":"bad and ugly teacher"}] var results = array.flatMap(obj => { const vals = Object.values(obj); return vals.flatMap(val => val.split(' ')); }) console.log(results);

Object.values just gets the value from each property of each object, so you're not dealing with the keys like 'full_name' or 'title1'

Then once you have the values, you split them by the space and return them as an array.

flatMap makes sure that if you return an array of arrays, it flattens it 1 layer.

edit -- here's it as a one liner:

var results = array.flatMap(obj => Object.values(obj).flatMap(val => val.split(' ')));

Assuming the values are always strings and probably the words could be separated by multiple spaces:

This v.split(/ +/) splits each phrase as an array of strings which will be concatenated to the main array (the accumulator in the reduce function).

 let array = [{"full_name":"louis jackson","title1":"english teacher","description":"good and nice teacher"},{"full_name":"peter carey","title1":"math teacher","description":"bad and ugly teacher"}]; let result = array.reduce((a, c) => { Object.values(c).forEach(v => a = a.concat(v.split(/ +/))); return a; }, []); console.log(result);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

Your solution is close, but you just have to concat the arrays instead of pushing the split arrays into results . Like this:

 let array = [{"full_name":"louis jackson","title1":"english teacher","description":"good and nice teacher"},{"full_name":"peter carey","title1":"math teacher","description":"bad and ugly teacher"}] let results = []; array.forEach(item => { results = results.concat(item.full_name.split(" ")) .concat(item.title1.split(" ")) .concat(item.description.split(" ")); }); console.log(results);

Here's another take using for..of and for..in so you don't need to manually define the property names:

 let array = [{"full_name":"louis jackson","title1":"english teacher","description":"good and nice teacher"},{"full_name":"peter carey","title1":"math teacher","description":"bad and ugly teacher"}] let results = []; for(const item of array) for(const prop in item) results.push(...item[prop].split(" ")) console.log(results);

result is an empty array. The split() method returns an array. When you call push() on an array, you're adding an array to an array. Every time you call push, you add another array to the array. So you have an array of arrays. What you want is a single array with all the values.

So call split() on each value to get an array, iterate through the values, then call push() on the values of the array, not the array itself.

Try this

var results = [];
array.forEach(item => {
   var names = item.full_name.split(" ");
   names.forEach(i => result.push(i));

   var titles = item.title1.split(" ");
   titles.forEach(t => result.push(t));

}

You can take @Klaycon advice and use

.concat() instead of forEach loop. If you're not using an older version of IE you can use ... a ssign operator .

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