Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8583613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T21:34:44+00:00 2026-06-11T21:34:44+00:00

This is a simple problem with a number of simple solutions, and I’m trying

  • 0

This is a simple problem with a number of simple solutions, and I’m trying to figure out which is best. I would like a solution which is terse and readable.

Here’s the issue. I have an array of objects, and I want to combine several of the object members into pipe separated strings. As an example for this data:

[
    {
        foo: 1,
        bar: 2
    },
    {
        foo: 10,
        bar: 20
    }
]

I want to be able to create strings like this:

foo = "1|10";
bar = "2|20";

If the items were stored in separate arrays, this would be as simple as using Array.join.

Here’s my current solution:

var foo = "";
var bar = "";

var firstItem = obj.splice(0,1)[0];
foo = firstItem.foo.toString();
bar = firstItem.bar.toString();

obj.forEach(function (item) {
    foo += "|" + item.foo.toString();
    bar += "|" + item.bar.toString();
​});

I also considered the following solution using Array.reduce, but browser support for this is still lacking. I am using Prototype, and it’s Array.reduce is unfortunately a completely different function than the native JS implementation.

var strings = obj.reduce(function (a, b) {
    return {
        a.foo.toString() + "|" + b.foo.toString(),
        a.bar.toString() + "|" + b.bar.toString()
    };
});

// value of strings is now:
// {
//     foo: "1|10",
//     bar: "2|20"
// }   

Are there any more elegant ways to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T21:34:46+00:00Added an answer on June 11, 2026 at 9:34 pm

    You could do this:

    function propsArrayJoin(arr, prop, glue) {
      var results = [];
      arr.forEach(function (item) {
        results.push(item[prop].toString());
    ​ });
      return results.join(glue);
    }
    
    obj = [
        {
            foo: 1,
            bar: 2
        },
        {
            foo: 10,
            bar: 20
        }
    ];
    
    foo = propsArrayJoin(obj, 'foo', '|');
    bar = propsArrayJoin(obj, 'bar', '|');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys having this really simple problem but cant seem to figure out have
This simple problem is kiling me. I posted something earlier about trying to clean
I can't seem to find solutions to this supposedly simple problem /bug, so here
this may seem like a simple problem but I couldn't find it in the
This is a really simple problem, but I couldn't find a solution anywhere. I'm
This is I think a simple problem but not getting the solution yet. I
It seems this is a common question/problem but despite checking out a number of
I have one obviously simple problem, but cannot find good solution, so I would
Quite simple problem (but difficult solution): I got a string in PHP like as
Been struggling with this simple selector problem a couple of hours now and must

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.