Solved

Split Sting in ArrayCollection

Posted on 2011-02-28
6
557 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
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!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need help Creating PowerShell Script 4 66
IDE for Python 5 105
Understanding ping command in Ubuntu 5 64
form isn't working is it missing a document 21 64
This article will show, step by step, how to integrate R code into a R Sweave document
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

734 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