0

I have the following dynamically generated array:

var myArray = ("0% { left:74px; top:202px; }"   , "44% { left:427px; top:122px; }", "0% { font-size:11px; }", "55% { font-size:49px; }" );

There are 2 entries that have the same start value: 0%. How can I find this and combine it together:

form:
0% { left:74px; top:202px; },
0% { font-size:11px; },
to
0% { left:74px; top:202px; font-size:11px;},

Thank you

Edited the code to a valid array.

16
  • 2
    Is that a valid javascript array ? Commented Sep 25, 2010 at 11:14
  • this is a part from my array. Should be valid with [] Commented Sep 25, 2010 at 11:19
  • there was a point between 55 and {. I had removed that. the array should be valid Commented Sep 25, 2010 at 11:21
  • It's still not a valid array. Commented Sep 25, 2010 at 12:09
  • What's that XX % thing? Is that some of animation or something? Commented Sep 25, 2010 at 12:20

2 Answers 2

2

That is an array literal with only one member. You need to end the strings between the commas to separate the members. I would try to rewrite each member as an object literal and use json methods to alternate between string and object. In object form, you can do a mixin to merge the like members.

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

Comments

1

oh, I see.. you're printing your array and this is what you're getting, right?

In that case...

for(var i =0; i< myArray.length ; i++){
  for(var j=i+1; j<myArray.length;j++){
      if(i == j) continue;
      if(myArray[i].substring(0,3) == myArray[j].substring(0,3)){//found matching first 2 chars
         myArray[i] = myArray[i].substring(0,3) + myArray[j].replace(/\{(.*?)\}/,"$1 ;") + myArray[i].substring(4);

        myArray.splice(j--,1);//remove the doup and decrease the counter so you don't skip one now that the array is shorter

      }
  }
}

I didn't test this, but something to this effect :)

Oh.. you edited. What you have now is definitely not an array... but you're closer than you were before.

I think array is a reserved word...

var myArray = ("0% { left:74px; top:202px; }"   , "44% { left:427px; top:122px; }", "etc", "etc" );

2 Comments

Sorry, the code have an error. I can't tell why... I can make my array like this: var myArray = ("0% { left:74px; top:202px; }", "44% { left:427px; top:122px; }", "etc", "etc" );
my code might have bugs, but it's what you should do in theory... first make your array look right (as I show at the bottom of my answer). Then loop through the array looking for matches on the first few characters and merge them together and then splice out the duplicate.

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.