Solved

Split Sting in ArrayCollection

Posted on 2011-02-28
6
554 Views
Last Modified: 2012-06-21
I have arraycollection items contains list of items;

I need to get the id and the description out of this list to be used in Flex app.

The input is:
                  private var input:ArrayCollection=new ArrayCollection
                  ([
                  { item: "5 Computers HP 125"},
                  { item: "125 Monitors LG 54 "},
                  { item: "00425 Keyboard AA 58 "},
                  { item: "0525 others "}
                  ]);


The output should be
                  private var output:ArrayCollection=new ArrayCollection
                  ([
                  { id: "5", Desc: "Computers HP 125"},
                  { id: "125", Desc: "Monitors LG 54"},
                  { id: "00425", Desc: " Keyboard AA 58"},
                  { id: "0525", Desc: “others"}
                  ]);
0
Comment
Question by:myBlueSky
6 Comments
 
LVL 42

Assisted Solution

by:sedgwick
sedgwick earned 175 total points
Comment Utility
i'm not infornt of my pc, hope its correct.
private var input:ArrayCollection=new ArrayCollection
                  ([
                  { item: "5 Computers HP 125"},
                  { item: "125 Monitors LG 54 "},
                  { item: "00425 Keyboard AA 58 "},
                  { item: "0525 others "}
                  ]);


 var newArray:ArrayCollection = new ArrayCollection();

for each( item in input )
   {
var idtoken:String = item["item"].split(" ")[0];

   var obj:Object = {id:idtoken, desc:item["item"].substr(0, idtoken.length)};

      newArray.addItem(obj);
   }

Open in new window

0
 
LVL 17

Accepted Solution

by:
deepanjandas earned 200 total points
Comment Utility
Try this:
var count:int = input.length;
var id:String;
var desc:String;
var index:int;
var value:String;

for( var i:int=0; i <input.length; i++ ){
    value = input.getItemAt(i);
    index = value.indexOf(" ");
    id = value.substr(0,index);
    desc = value.substr(index+1);
    input.setItemAt({ id: id, Desc: desc}, i);

}

Open in new window


Warm Regards
Deepanjan Das
0
 
LVL 17

Expert Comment

by:deepanjandas
Comment Utility
Cool @sedgwick has answered it already.
But I think mine will be better in performance as it uses the same ArrayCollection :)

Warm Regards
Deepanjan Das
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Author Comment

by:myBlueSky
Comment Utility

Deepanjan
I’m getting error on this statement:  value = input.getItemAt(i);

Implicit coercion of a value with static type Object to a possibly unrelated type String
0
 
LVL 4

Assisted Solution

by:ute_arbeit
ute_arbeit earned 125 total points
Comment Utility
You could use a regular expression for splitting the items and a callback function for mapping the input objects to the output objects like this:

private var output:ArrayCollection = new ArrayCollection (input.toArray().map(splitItems));
       
function splitItems(element:*, index:int, array:Array):Object {
   var matches:Array = element.item.match(/^([0-9]+) (.*)$/);
   return {id:matches[1], Desc:matches[2]};
}

Open in new window

0
 
LVL 17

Expert Comment

by:deepanjandas
Comment Utility
Set the value to an Object;

var value:Object;

Also @ute_arbeit 's suggestion is good.

Warm Regards
Deepanjan Das

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now