Solved

Split Sting in ArrayCollection

Posted on 2011-02-28
6
556 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
ID: 34995201
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
ID: 34995255
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
ID: 34995263
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:myBlueSky
ID: 34995771

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
ID: 34995784
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
ID: 34995973
Set the value to an Object;

var value:Object;

Also @ute_arbeit 's suggestion is good.

Warm Regards
Deepanjan Das

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

820 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