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

  • SEARCH
  • Home
  • 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 9241279
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:20:35+00:00 2026-06-18T08:20:35+00:00

I have an array like this: [one, foo, two, baz, two, one, three, lulz,

  • 0

I have an array like this:

["one", "foo", "two", "baz", "two", "one", "three", "lulz", "wtf", "three"]
   1             2             2      1       3                       3

Each string is a key to access an object literal. What I want to do is prepend the full path to children keys delimited by main keys in this case one, two, and three. This is the expected result:

[
  'one', 'one.foo', 'one.two', 'one.two.baz', 'one.two', 'one',
  'three', 'three.lulz', 'three.wtf', 'three'
]

I’ve been trying for past few hours with loops and extracting the duplicated items first and then trying to slice the array by a given start and end points. Then I tried with regex, just to see if it was possible but JS doesn’t handle recursion in regular expressions, and it got very complicated and probably unnecesary. I’m at that point where I got frustrated and nothing is working. I’ve no clue where to go now. I’m stuck.

Edit: I’ve been trying the above for a while because it seemed simpler but ideally all I want to extract is the chain of keys without the closing “tags”.

[
  'one', 'one.foo', 'one.two', 'one.two.baz',
  'three', 'three.lulz', 'three.wtf'
]

Can somebody enlighten me here?

  • 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-18T08:20:36+00:00Added an answer on June 18, 2026 at 8:20 am

    Without recursion you can solve it like this; it keeps a common prefix and at every iteration it scans both left and right to find an identical item.

    If there’s an identical item to the left, reduce the prefix (before emit); if there’s an item to the right, increase the prefix (after emit).

    var a = ["one", "foo", "two", "baz", "two", "one", "three", "lulz", "wtf", "three"];
    
    var prefix = '',
        keys = [];
    
    for (var i = 0, len = a.length; i < len; ++i) {
      var left = a.indexOf(a[i]),
          right = a.indexOf(a[i], i + 1);
    
      if (left >= 0 && left < i) {
        prefix = prefix.substr(0, prefix.lastIndexOf(a[i]));
      }
      keys.push(prefix + a[i]);
      if (right !== -1) {
        prefix = prefix + a[i] + '.';
      }
    }
    
    console.log(keys);
    

    Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array like this (one dimension only): $arr = array('one', 'two', 'three',
Lets say I have string like this: String q = foo (one) bla (two)
I have converted an array to object data like this: <?php $myobject->data = (object)Array('zero','one','two');
Lets say I have 2 arrays . $arr1=array(foo=>bar, 1=>one, 2=>two); $arr2=array(h=>eich, 3=>three, 4=>four); By
I have an array like this one $state[1]='FG'; $state[2]='BU'; $state[3]='CA'; ... $state[150]='YT' What I
Suppose I have a one-dimensional array $arr like this: Array ( ['key1'] => 1
I have an array like this: array( 0 => array(src => foo-21.gif), 1 =>
I have one route that looks like this: Router::connect('/Album/:slug/:id',array('controller' => 'albums', 'action' => 'photo'),array('pass'
I have an array like this numpy array dd= [[foo 0.567 0.611] [bar 0.469
I have one array that contains some settings that looks like basically like this:

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.