Solved

Split Sting in ArrayCollection

Posted on 2011-02-28
6
558 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
Industry Leaders: 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

Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

615 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