skip to Main Content

I’m having this below array, where I need to update the key value as
From Apple fruit to Pizza shop, Orange fruit to Kfc shop, Banana fruit to Mcdonald shop, Mango fruit to fries shop if any one the value pair exceeds 28 characters, if it doesn’t exceed 28 characters, it should remain the same response.

For instance in this Testing 3 has value that exceeds 28 characters

 [
        {
            "key": "Apple fruit",
            "value": "66789/74657/37485/67543"
        },
        {
            "key": "Orange fruit",
            "value": "66789/74657/37485/67543"
        },
        {
            "key": "Banana fruit",
            "value": "3647586/456765/345654/36789876/76"
        },
        {
            "key": "Mango fruit",
            "value": "66789/74657/37485/67543"
        }
    ]

My code

    this.testItem.push({
      key: testlabel,
      value: testvale,
    });
    console.log(this.testItem);

this.testItem has the array of value I have mentioned above, any help ?

4

Answers


  1. You could do something like this:

    this.testItem =  [
            {
                "key": "Testing 1",
                "value": "66789/74657/37485/67543"
            },
            {
                "key": "Testing 2",
                "value": "66789/74657/37485/67543"
            },
            {
                "key": "Testing 3",
                "value": "3647586/456765/345654/36789876/76"
            },
            {
                "key": "Testing 4",
                "value": "66789/74657/37485/67543"
            }
        ];
    
    const hasMoreThan28Chars = this.testItem.some(item => item.value.length > 28);
    
    this.testItem = !hasMoreThan28Chars ? this.testItem : this.testItem.map((item) => 
        ({...item, key: item.key.replace("Testing", "Test")})
    );
    
    console.log(this.testItem)
    
    Login or Signup to reply.
  2. Your initial specs are a bit unclear, but regardless. Here is the code that will achieve what you are requesting:

    // Your array - don't add this 
    this.testItem = [
            {
                "key": "Testing 1",
                "value": "66789/74657/37485/67543"
            },
            {
                "key": "Testing 2",
                "value": "66789/74657/37485/67543"
            },
            {
                "key": "Testing 3",
                "value": "3647586/456765/345654/36789876/76"
            },
            {
                "key": "Testing 4",
                "value": "66789/74657/37485/67543"
            }
        ];
    
    
    // Add the code below to modify your array `this.testItem`
    var hasValueExceeding28Chars = this.testItem.some(item => item.value.length > 28);
    
    this.testItem = hasValueExceeding28Chars
      ? this.testItem.map(item => ({
          key: item.key.replace(/Testing/g, "Test"),
          value: item.value
        }))
      : this.testItem;
    
    console.log(this.testItem);

    Here we are:

    1. Finding out whether any of the values exceed 28 characters;
    2. If so, we are using the map() function of Javascript to copy the initial into a new array and modifying all the keys accordingly;
    3. If not, then we maintain the initial array.
    Login or Signup to reply.
  3. in place of hello write code to change value

    testArray.forEach(test => { if(test.value.length > 28 )test.key='hello'});
    
    Login or Signup to reply.
  4. testItem = testItem.map(item => {
      if (item.value.length > 28) {
        const num = item.key.substring(item.key.length - 1)
        item.key = `Test ${num}`
      }
      return item
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search