Mapping- Separate Javascript Objects and then Append the Last Object

时间:2015-05-12 23:13:05

标签: javascript algorithm

There is a quite bit challenge to handle the following problem.

I have the following array and javascript objects:

Input:

I have three different colors chmod and three different sizes (blue,green,yellow) in the javascript objects and trying to map them.

(sz=9,11,13)

Output:

color=["blue","green","yellow"];

myObj=[{x:0,y:1,sz:9},{x:4,y:11,sz:9},{x:11,y:17,sz:11},{x:29,y:18,sz:13}];

Here is the jsfiddle which map objects based on their data1:[{x:0,y:1,sz:9,color:"blue"},{x:4,y:11,sz:9,color:"blue"}]; data2:[{x:4,y:11,sz:9,color:"green"},{x:11,y:17,sz:11,color:"green"}]; data3:[{x:11,y:17,sz:11,color:"yellow"},{x:29,y:18,sz:13,color:"yellow"}]; property and adds sz properties. The only thing is missing is to add the last object and change color.

For example:

color's first object comes from data2's last object except different color which is green.

Here is what I have :

data1

Here is what I need :

data2:[{x:11,y:17,sz:11,color:"green"}];

1 个答案:

答案 0 :(得分:1)

I think you need something like that:

Sorry for that, i deleted the old code.

UPDATE 3:

https://jsfiddle.net/kxhsapad/3/

OLD REQUEST:

function compose(f1, f2)
{
   return function(val) {
      return f1(val)&&f2(val);
   }
}
// in this case the .filter traverses the array only once, 
// instead of twice, i.e once for each subfilter function
result2 = animals.filter(compose(letterFilter, lengthFilter));

Please check jsfiddle for full code https://jsfiddle.net/kxhsapad/