0

I have two object now

obj1-> [ 'logo', 'FinTech startup', 'design' ]
obj2-> [ 'logo', 'tech startup', 'design' ]

What is the fastest way to turn them into

obj1-> [ 'logo', 'FinTech', 'startup', 'design' ]
obj2-> [ 'logo', 'tech', 'startup', 'design' ]

Please?

Thanks!!

8
  • Are obj1 and obj 2 just arrays or are they objects with a named parameter that contains an array? Commented Mar 25, 2017 at 17:13
  • newObj = obj1.join(' ').split(' '); feel hackish but works Commented Mar 25, 2017 at 17:13
  • Please share your code as well. Just showing input and expected output is not good enough. Commented Mar 25, 2017 at 17:20
  • @Christopher no they don't have any parameter name.. Commented Mar 25, 2017 at 17:23
  • @barry-johnson thanks! Commented Mar 25, 2017 at 17:23

1 Answer 1

3

You could use Array#join and Array#split with space.

var array1 = ['logo', 'FinTech startup', 'design'],
    array2 = ['logo', 'tech startup', 'design'];
    
array1 = array1.join(' ').split(' ');
array2 = array2.join(' ').split(' ');

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

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

2 Comments

I guess .reduce can also be used. result = arr1.reduce((p,c) => p.concat(c.split(" ")))
@Rajesh true but it's a lot more iteration and actually doing the opposite of what reduce is meant for (reducing to a single value)

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.