1

If I have an array,I want to extract each values in each line (values after (:) )

Array =
0:Object {node-input-audio_services1: "79388", audio_ip: "127.0.0.1", audio_port_1: "7654"}
1:Object {node-input-video_services1: "80788", video_ip: "127.0.0.1", video_port_1: "7654"}
2:Object {node-input-audio_services1: "4fe10"}
3:Object {node-input-video_services1: "4fe10"}

Imagine I have several key : value then I want to extract Just value like bellow:

Array =
0:Object {key1 : "value" , Key2 : "value2" , key3: "value3"}
1:Object {key1 : "value5" , Key2 : "value6" , key3: "value7"}

I have the result like this :

var result = ["value","value1",value3","value5","value6","value7"];

The problem is, I can't use (key1.array) because each time in my program have one key's name.I really new in JavaScript and don't know how I can start

2 Answers2

0

I think you should do a for loop to go through the Array first.

for (let i = 0; i <= array.length; i++{
}

and check in every line, doing something like:

if (idExample == array.id[i]){
  return idExample
}
james
  • 110
  • 1
  • 11
  • The problem is,I have "0:" with this code can't go in side the my array, my error is : "Cannot read property '0' of undefined" – faezanehhabib Feb 15 '20 at 12:03
0

You will need create a function like "findWithAttr".

Try this step by step below after create the "findWithAttr" function.

function findWithAttr(array, attr, value) {
  for(var i = 0; i < array.length; i += 1) {
    if(array[i][attr] === value) {
      return i;
    }
  }
  return -1;
}

const allLines = [
  {id: "29605581.57644a", type: "tab", label: "Flow 1"},
  {id: "f17a4a1e.6513f8", type: "tab", label: "Flow 2"},
  {id: "6730d437.0698dc", type: "tab", label: "Flow 3"},
  {id: "89b04ddc.4d32e", type: "input-distributer", z: "29605581.57644a"},
  {id: "74d8cc97.780304", type: "video-switcher", z: "29605581.57644a"},
  {id: "5555183d.d59408", type: "output-distributer", z: "29605581.57644a"},
  {id: "29605581.57644a", type: "output-distributer", z: "29605581.57644a"}
]

// Get clicked line
const clickedLine = {id: "29605581.57644a", type: "tab", label: "Flow 1"}

// Use a "findWithAttr" function to get clicked line's index
const clickedLineIndex = findWithAttr(allLines, clickedLine.id)

// Then filter other lines
const otherLines = allLines.filter((line, index) => index !== clickedLineIndex)

// Finally get lines with equal IDs
const linesWithEqualId = otherLines.filter((line, i) => line.id === clickedLine.id)

// And log the result
console.log(linesWithEqualId)