Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 560
  • Last Modified:

Split Sting in ArrayCollection

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
myBlueSky
Asked:
myBlueSky
3 Solutions
 
Meir RivkinFull stack Software EngineerCommented:
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
 
deepanjandasCommented:
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
 
deepanjandasCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
myBlueSkyAuthor Commented:

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
 
ute_arbeitCommented:
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
 
deepanjandasCommented:
Set the value to an Object;

var value:Object;

Also @ute_arbeit 's suggestion is good.

Warm Regards
Deepanjan Das

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now